70

Does anyone know how to change the from user when sending email using the mail command? I have looked through the man page and can not see how to do this.

We are running Redhat Linux 5.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
Joel Cunningham
  • 13,620
  • 8
  • 43
  • 49

15 Answers15

80

You can specify any extra header you may need with -a

$mail -s "Some random subject" -a "From: some@mail.tld" to@mail.tld
daniels
  • 18,416
  • 31
  • 103
  • 173
  • 5
    @michabbb not working on my Debian Wheezy, result: "From: some@mail.tld: No such file or directory" – baptx Feb 22 '15 at 19:41
  • Have you wrapped your param in quotes as in the example above? – daniels Feb 24 '15 at 10:01
  • 3
    yes with quotes like your example and it displays the error I wrote. But the mail -r option is working https://stackoverflow.com/questions/119390/specify-the-from-user-when-sending-email-using-the-mail-command#comment-45619737 (btw don't forget the @username mention if you you want people to get notifications about your replies) – baptx Feb 26 '15 at 21:39
  • also working on ubuntu 14.04 (`/etc/alternatives/mail -> /usr/bin/bsd-mailx`), but not on 16.04 (`/etc/alternatives/mail -> /usr/bin/s-nail`) – Phillip -Zyan K Lee- Stockmann Feb 01 '17 at 09:24
  • Works on Ubuntu Xenial after `apt-get install mailutils`. – Gogowitsch Mar 18 '19 at 18:56
30

http://www.mindspill.org/962 seems to have a solution.

Essentially:

echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -- -f from_user@example.com

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 19
    Doesn't seem to pass the sniff-test. `--` marks end of options to `getopt(3)`, so how would `-f` possibly work as an option after `--`? Also, reading the man page for (bsd) `mail` on Ubuntu doesn't mention any such `-f` argument support. Lastly, testing this on Ubuntu 14.04 (with some disbelief and slight hope) shows that indeed it doesn't work. FWIW (hard to believe again) the original mindspill.org URL says it works on Red Hat without specifying a version. – arielf Jan 14 '16 at 01:11
  • Use the `-v` option for Verbose output `mail -v -s ...`. If the mail fails to deliver due to an improperly configured mail server for example, the smtp command log will show what has gone wrong. – Andrei Krasutski Jul 07 '17 at 06:27
  • in debian 9 -f is used to pass a filename – user1077915 Sep 22 '17 at 12:12
27

mail -r from@from.from -R from@from.com

-r = from-addr -R = reply-to addr

The author has indicated his version of mail doesn't support this flag. But if you have a version that does this works fine.

bfabry
  • 1,874
  • 1
  • 13
  • 21
21

When sending over SMTP, the mail man page advises to set the from variable, in this way (Tested on CentOS 6):

mail -s Subject -S from=sender@example.com recipient@example.com

You could also attach a file using the -a option:

mail -s Subject -S from=sender@example.com -a path_to_attachement recipient@example.com
G.J
  • 493
  • 4
  • 10
7

None of these worked for me (Ubuntu 12.04) but finally with trial & error I got:

echo 'my message blabla\nSecond line (optional of course)' | 
mail -s "Your message title"
-r 'Your full name<yourSenderAdress@yourDomain.abc>'
-Sreplyto="yourReplyAdressIfDifferent@domain.abc"
destinatorEmail@destDomain.abc[,otherDestinator@otherDomain.abc]

(all in one line, there is no space in "-Sreplyto")

I got this mail command from:

apt-get install mailutils
Le Droid
  • 4,534
  • 3
  • 37
  • 32
5

You can append sendmail options to the end of the mail command by first adding --. -f is the command on sendmail to set the from address. So you can do this:

mail recipient@foo.com -- -f sender@bar.com

Gerald
  • 23,011
  • 10
  • 73
  • 102
4

None of the above worked for me. And it took me long to figure it out, hopefully this helps the next guy.

I'm using Ubuntu 12.04 LTS with mailutils v2.1.

I found this solutions somewhere on the net, don't know where, can't find it again:

-aFrom:Servername-Server@mydomain.com

Full Command used:

cat /root/Reports/ServerName-Report-$DATE.txt | mail -s "Server-Name-Report-$DATE" myemailadress@mydomain.com -aFrom:Servername-Server@mydomain.com
user1251007
  • 15,891
  • 14
  • 50
  • 76
Hardus
  • 41
  • 2
2

Here's a solution.

The second easiest solution after -r (which is to specify a From: header and separate it from the body by a newline like this

 $mail -s "Subject" destination@example.com
 From: Joel <joel@example.com>

 Hi!
 .

works in only a few mail versions, don't know what version redhat carries).

PS: Most versions of mail suck!

Community
  • 1
  • 1
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
2

Most people need to change two values when trying to correctly forge the from address on an email. First is the from address and the second is the orig-to address. Many of the solutions offered online only change one of these values.

If as root, I try a simple mail command to send myself an email it might look like this. echo "test" | mail -s "a test" me@noone.com

And the associated logs: Feb 6 09:02:51 myserver postfix/qmgr[28875]: B10322269D: from=<root@myserver.com>, size=437, nrcpt=1 (queue active) Feb 6 09:02:52 myserver postfix/smtp[19848]: B10322269D: to=<me@noone.com>, relay=myMTA[x.x.x.x]:25, delay=0.34, delays=0.1/0/0.11/0.13, dsn=2.0.0, status=sent (250 Ok 0000014b5f678593-a0e399ef-a801-4655-ad6b-19864a220f38-000000)

Trying to change the from address with -- echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com

This changes the orig-to value but not the from value: Feb 6 09:09:09 myserver postfix/qmgr[28875]: 6BD362269D: from=<root@myserver.com>, size=474, nrcpt=2 (queue active) Feb 6 09:09:09 myserver postfix/smtp[20505]: 6BD362269D: to=<me@noone>, orig_to=<dude@thisguy.com>, relay=myMTA[x.x.x.x]:25, delay=0.31, delays=0.06/0/0.09/0.15, dsn=2.0.0, status=sent (250 Ok 0000014b5f6d48e2-a98b70be-fb02-44e0-8eb3-e4f5b1820265-000000)

Next trying it with a -r and a -- to adjust the from and orig-to. echo "test" | mail -s "a test" -r dude@comeguy.com me@noone.com -- dude@someguy.com

And the logs: Feb 6 09:17:11 myserver postfix/qmgr[28875]: E3B972264C: from=<dude@someguy.com>, size=459, nrcpt=2 (queue active) Feb 6 09:17:11 myserver postfix/smtp[21559]: E3B972264C: to=<me@noone.com>, orig_to=<dude@someguy.com>, relay=myMTA[x.x.x.x]:25, delay=1.1, delays=0.56/0.24/0.11/0.17, dsn=2.0.0, status=sent (250 Ok 0000014b5f74a2c0-c06709f0-4e8d-4d7e-9abf-dbcea2bee2ea-000000)

This is how it's working for me. Hope this helps someone.

Jerad
  • 21
  • 1
1

on CentOs5: -r from@me.omg

fun_vit
  • 384
  • 1
  • 3
  • 12
1
echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -- -f from_user@example.com -F "Elvis Presley"

or

echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -aFrom:"Elvis Presley<from_user@example.com>"
1

This works on Centos7

echo "This is the main body of the mail" | mail -s "Subject of the Email" -r seneder_address@whatever.com recipent_address@example.com

1

Here's an answer from 2018, on Debian 9 stretch.

Note the -e for echo to allow newline characters, and -r for mailx to show a name along with an outgoing email address:

$ echo -e "testing email via yourisp.com from command line\n\nsent on: $(date)" | mailx -r "Foghorn Leghorn <sender@yourisp.com>" -s "test cli email $(date)" -- recipient@somedomain.com

Hope this helps!

scrat.squirrel
  • 3,607
  • 26
  • 31
1

For CentOS here is the working command :

mail -s Subject -S from=sender@example.com recipient@example.com
0

Thanks to all example providers, some worked for some not. Below is another simple example format that worked for me.

echo "Sample body" | mail -s "Test email" from=sender-addrs@example.com recepient-addres@example.com
hata
  • 11,633
  • 6
  • 46
  • 69
ayon
  • 1