103

Sending a message from the Unix command line using mail TO_ADDR results in an email from $USER@$HOSTNAME. Is there a way to change the "From:" address inserted by mail?

For the record, I'm using GNU Mailutils 1.1/1.2 on Ubuntu (but I've seen the same behavior with Fedora and RHEL).

[EDIT]

$ mail -s Testing chris@example.org                                                                  
Cc: 
From: foo@bar.org

Testing
.

yields

Subject: Testing
To: <chris@example.org>
X-Mailer: mail (GNU Mailutils 1.1)
Message-Id: <E1KdTJj-00025z-RK@localhost>
From: <chris@localhost>
Date: Wed, 10 Sep 2008 13:17:23 -0400

From: foo@bar.org

Testing

The "From: foo@bar.org" line is part of the message body, not part of the header.

Chris Conway
  • 55,321
  • 43
  • 129
  • 155
  • 1
    What flavor of Unix is this? and which mail version? Just to know where that doesn't work. – Vinko Vrsalovic Sep 10 '08 at 17:25
  • Um, almost all. Anyone using mailx or berkeley mail is going to see it that way. If you want to affect the header, write the whole header and send with rmail or sendmail directly. – Thomas Kammeyer Sep 10 '08 at 17:33
  • Oh, but, sorry, I run GenToo. – Thomas Kammeyer Sep 10 '08 at 17:35
  • For the record, I'm using mailx and the example Chris gives works. Or is that what you meant? – Vinko Vrsalovic Sep 10 '08 at 18:12
  • Vinko, what version of UN*X are you running? Do you mean by "works" that you get his results or that you get the desired behavior that would address his need? It depends in some degree on how message submission works on your platform and whether the client adds a blank line before the – Thomas Kammeyer Sep 10 '08 at 18:49
  • From `man mail`: `-r address Sets the From address. Overrides any from variable specified in environment or startup files. ` – Kellen Stuart Apr 05 '17 at 22:49
  • The problem with archaic commands such as `mail` is that every damn version has different API. Different implementations may set `From` based on environment variables `$USER`, `$HOSTNAME`, `$EMAIL` or expect you to use `-f`, `-F`, `-a` or `-r` flags. And of course every flag is incompatible with some another version. For example, `-a` means add a new header for one implementation and add a file as MIME attachment for another implementation. POSIX defines only `mailx` and only `-s` flag: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/mailx.html – Mikko Rantalainen Feb 17 '23 at 11:13
  • If you want to GNU Mailutils, then you should write something like `mail.mailutils -a "From: user@domain.com" -s "Subject line here" "receiver@example.com"` instead of `mail` which may have any random API. – Mikko Rantalainen Feb 17 '23 at 11:14

20 Answers20

118

In my version of mail ( Debian linux 4.0 ) the following options work for controlling the source / reply addresses

  • the -a switch, for additional headers to apply, supplying a From: header on the command line that will be appended to the outgoing mail header
  • the $REPLYTO environment variable specifies a Reply-To: header

so the following sequence

export REPLYTO=cms-replies@example.com
mail -aFrom:cms-sends@example.com -s 'Testing'

The result, in my mail clients, is a mail from cms-sends@example.com, which any replies to will default to cms-replies@example.com

NB: Mac OS users: you don't have -a , but you do have $REPLYTO

NB(2): CentOS users, many commenters have added that you need to use -r not -a

NB(3): This answer is at least ten years old(1), please bear that in mind when you're coming in from Google.

cms
  • 5,864
  • 2
  • 28
  • 31
  • I just tested it again here to make sure, and it works fine for me. Not all mail clients work well with Reply-To, but I'd have thought that was a solved problem by now. The REPLYTO env variable is mentioned in the man page, Other UNIX mailers honour it, emacs etc. Still, I guess you have a fix. – cms Dec 19 '08 at 15:23
  • I don't think it's the mail client... I don't see the Reply-To header in the raw message text. But, yeah, -a is sufficient. – Chris Conway Dec 19 '08 at 16:43
  • 1
    No, it doesn't. But: "The complete GNU mailutils manual is not available in Debian systems due to licensing reasons." -aReply-To:... works. – Chris Conway Dec 19 '08 at 19:11
  • I also don't see any mention of REPLYTO at http://www.gnu.org/software/mailutils/manual/mailutils.html – Chris Conway Dec 19 '08 at 19:13
  • Thank you for replying. My /usr/bin/mail comes from the mailx package, which I think is derived from BSD-mail. And most of my unix machines are BSD. – cms Dec 19 '08 at 21:33
  • This works for me on Ubuntu but I get `mail: invalid option -- a` on CentOS. – dave1010 Feb 06 '13 at 11:01
  • 4
    in CentOS the `mail -a` option means "attachemnet" . I needed to use `-r` like `mail -r from@whatever.com` – equivalent8 Feb 11 '19 at 10:25
42

On Centos 5.3 I'm able to do:

mail -s "Subject" user@address.com -- -f from@address.com < body

The double dash stops mail from parsing the -f argument and passes it along to sendmail itself.

Beau
  • 11,267
  • 8
  • 44
  • 37
  • 6
    does not work. 3 addresses get added to to address now. the to address, the f address and the localdomain – shorif2000 Jun 09 '14 at 15:43
  • 3
    Can you help, this used to work - but, since a recent update, it no longer works, it just tries additionally sending an email to -f@hostname – Wil Jan 15 '15 at 22:07
  • 1
    Doesn't work on CentOS 6.3. Try @ubuntu-fanboy answer below using the -r flag. – skytaker May 13 '15 at 21:22
28

GNU mailutils's 'mail' command doesn't let you do this (easily at least). But If you install 'heirloom-mailx', its mail command (mailx) has the '-r' option to override the default '$USER@$HOSTNAME' from field.

echo "Hello there" | mail -s "testing" -r sender@company.com recipient@company.com

Works for 'mailx' but not 'mail'.

$ ls -l /usr/bin/mail
lrwxrwxrwx 1 root root 22 2010-12-23 08:33 /usr/bin/mail -> /etc/alternatives/mail
$ ls -l /etc/alternatives/mail
lrwxrwxrwx 1 root root 23 2010-12-23 08:33 /etc/alternatives/mail -> /usr/bin/heirloom-mailx
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ubuntu-fanboy
  • 511
  • 5
  • 3
  • 7
    Maybe could be useful to know that GNU mailutils `mail` command preinstalled on Ubuntu 14.04 LTS supports `-r` option, so you can easily set the sender address. – gerlos Jun 04 '15 at 17:17
  • I had tested on Debian 9.11, and the option is also works. – Fedir RYKHTIK Mar 12 '20 at 16:11
16
mail -s "$(echo -e "This is the subject\nFrom: Paula <johny@paula.com>\n
Reply-to: 1232564@yourserver.com\nContent-Type: text/html\n")" 
milas.josh@gmail.com < htmlFileMessage.txt

the above is my solution....any extra headers can be added just after the from and before the reply to...just make sure you know your headers syntax before adding them....this worked perfectly for me.

MoSs
  • 261
  • 3
  • 3
  • 1
    This is working for me with your example.. . When i change the email address subject etc all of the headers are dispalyed? – bsmoo Jul 11 '13 at 16:11
  • This is the only way it works for me on MacOS (Monterey) to pass the "From:
    " header, Thanks for the tip!
    – Arul Selvan Jul 13 '22 at 18:18
8

Plus it's good to use -F option to specify Name of sender.

Something like this:

mail -s "$SUBJECT" $MAILTO -- -F $MAILFROM -f ${MAILFROM}@somedomain.com

Or just look at available options: http://www.courier-mta.org/sendmail.html

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
artickl
  • 81
  • 1
  • 1
5

It's also possible to set both the From name and from address using something like:

 echo test | mail -s "test" example@example.com -- -F'Some Name<example2@example.com>' -t

For some reason passing -F'Some Name' and -fexample2@example.com doesn't work, but passing in the -t to sendmail works and is "easy".

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alcanzar
  • 16,985
  • 6
  • 42
  • 59
5

I derived this from all the above answers. Nothing worked for me when I tried each one of them. I did lot of trail and error by combining all the above answers and concluded on this. I am not sure if this works for you but it worked for me on Ununtu 12.04 and RHEL 5.4.

echo "This is the body of the mail" | mail -s 'This is the subject' '<receiver-id1@email.com>,<receiver-id2@email.com>' -- -F '<SenderName>' -f '<from-id@email.com>'

One can send the mail to any number of people by adding any number of receiver id's and the mail is sent by SenderName from from-id@email.com

Hope this helps.

4

Here are some options:

  • If you have privelige enough, configure sendmail to do rewrites with the generics table

  • Write the entire header yourself (or mail it to yourself, save the entire message with all headers, and re-edit, and send it with rmail from the command line

  • Send directly with sendmail, use the "-f" command line flag and don't include your "From:" line in your message

These aren't all exactly the same, but I'll leave it to you look into it further.

On my portable, I have sendmail authenticating as a client to an outgoing mail server and I use generics to make returning mail come to another account. It works like a charm. I aggregate incoming mail with fetchmail.

Thomas Kammeyer
  • 4,457
  • 21
  • 30
4

I don't know if it's the same with other OS, but in OpenBSD, the mail command has this syntax:

mail to-addr ... -sendmail-options ...

sendmail has -f option where you indicate the email address for the FROM: field. The following command works for me.

mail recepient@example.com -f from@example.com
  • Works for me! (Also openBSD -- Macbook pro.) – Aeonaut Mar 28 '12 at 02:23
  • 1
    Update -- the email recipient still sees my local account name before `from@example.com` -- e.g., "Aeonaut `from@example.com`". Any idea how to change this? – Aeonaut Mar 28 '12 at 02:35
  • 3
    Does not work for me. (os x mountain lion) "mail: Cannot give -f and people to send to." – Jonny May 27 '13 at 05:54
3

On CentOS this worked for me:

echo "email body" | mail -s "Subject here" -r from_email_address email_address_to
Céline Aussourd
  • 10,214
  • 4
  • 32
  • 36
3

On Debian 7 I was still unable to correctly set the sender address using answers from this question, (would always be the hostname of the server) but resolved it this way.

Install heirloom-mailx

apt-get install heirloom-mailx

ensure it's the default.

update-alternatives --config mailx

Compose a message.

mail -s "Testing from & replyto" -r "sender <sender@example.com>" -S replyto="sender@example.com" recipient@example.net < <(echo "Test message")
jelloir
  • 155
  • 2
  • 12
3

Thanks BEAU

mail -s "Subject" user@address.com -- -f from@address.com

I just found this and it works for me. The man pages for mail 8.1 on CentOS 5 doesn't mention this. For -f option, the man page says:

-f Read messages from the file named by the file operand instead of the system mailbox. (See also folder.) If no file operand is specified, read messages from mbox instead of the system mailbox.

So anyway this is great to find, thanks.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
gbla
  • 31
  • 1
  • 1
    `-f` is of course not an option for `mail` in your example, as it is preceded by `--`. That option is handed over to your MTA – Anthon Feb 08 '16 at 06:55
1

echo "body" | mail -S from=address@foo.com "Hello"

-S lets you specify lots of string options, by far the easiest way to modify headers and such.

1

echo "test" | mailx -r fake@example.com -s 'test' email@example.com

It works in OpenBSD.

J. Ceron
  • 1,188
  • 10
  • 8
0

this worked for me

echo "hi root"|mail -rsawrub@testingdomain.org -s'testinggg' root
axel22
  • 32,045
  • 9
  • 125
  • 137
0

What allowed me to have a custom reply-to address on an Ubuntu 16.04 with UTF-8 encoding and a file attachment:

Install the mail client:

sudo apt-get install heirloom-mailx

Edit the SMTP configuration:

sudo vim /etc/ssmtp/ssmtp.conf
mailhub=smtp.gmail.com:587
FromLineOverride=YES
AuthUser=???@gmail.com
AuthPass=???
UseSTARTTLS=YES

Send the mail:

sender='send@domain.com'
recipient='recipient@domain.com'
zipfile="results/file.zip"
today=`date +\%d-\%m-\%Y`
mailSubject='My subject on the '$today
read -r -d '' mailBody << EOM
Find attached the zip file.

Regards,
EOM
mail -s "$mailSubject" -r "Name <$sender>" -S replyto="$sender" -a $zipfile $recipient < <(echo $mailBody)
Stephane
  • 11,836
  • 25
  • 112
  • 175
0

None of the above solutions are working for me...

#!/bin/bash

# Message
echo "My message" > message.txt

# Mail
subject="Test"
mail_header="From: John Smith <john.smith@example.com>"
recipients="recipient@example.com"

#######################################################################
cat message.txt | mail -s "$subject" -a "$mail_header" -t "$recipients"
JazzCat
  • 4,243
  • 1
  • 26
  • 40
0

I recent versions of GNU mailutils mail it is simply mail -r foo@bar.com.

Looking at the raw sent mail, it seems to set both Return-Path: <foo@bar.com> and From: foo@bar.com.

bluenote10
  • 23,414
  • 14
  • 122
  • 178
0

On CentOS 5.5, the easiest way I've found to set the default from domain is to modify the hosts file. If your hosts file contains your WAN/public IP address, simply modify the first hostname listed for it. For example, your hosts file may look like:

...
11.22.33.44 localhost default-domain whatever-else.com
...

To make it send from whatever-else.com, simply modify it so that whatever-else.com is listed first, for example:

...
11.22.33.44 whatever-else.com localhost default-domain
...

I can't speak for any other distro (or even version of CentOS) but in my particular case, the above works perfectly.

papsy
  • 433
  • 4
  • 5
-1

The answers provided before didn't work for me on CentOS5. I installed mutt. It has a lot of options. With mutt you do this this way:

export EMAIL=myfrom@example.com
export REPLYTO=myreplyto@example.com
mutt -s Testing chris@example.org
keypress
  • 759
  • 9
  • 12