Specify the from user when sending email using the mail command

LinuxEmailRedhat

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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

$mail -s "Some random subject" -a "From: [email protected]" [email protected]

Solution 2 - Linux

<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" [email protected] -- -f [email protected]

Solution 3 - Linux

mail -r [email protected] -R [email protected]

-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.

Solution 4 - Linux

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

Solution 5 - Linux

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<[email protected]>'
-Sreplyto="[email protected]"
[email protected][,[email protected]]

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

I got this mail command from:

apt-get install mailutils

Solution 6 - Linux

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 [email protected] -- -f [email protected]

Solution 7 - Linux

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" [email protected] -aFrom:[email protected]

Solution 8 - Linux

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" [email protected]
 From: Joel <[email protected]>

 Hi!
 .

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

PS: Most versions of mail suck!

Solution 9 - Linux

on CentOs5: -r [email protected]

Solution 10 - Linux

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<[email protected]>"

Solution 11 - Linux

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" [email protected]

And the associated logs: Feb 6 09:02:51 myserver postfix/qmgr[28875]: B10322269D: from=<[email protected]>, size=437, nrcpt=1 (queue active) Feb 6 09:02:52 myserver postfix/smtp[19848]: B10322269D: to=<[email protected]>, 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" [email protected] -- [email protected]

This changes the orig-to value but not the from value: Feb 6 09:09:09 myserver postfix/qmgr[28875]: 6BD362269D: from=<[email protected]>, size=474, nrcpt=2 (queue active) Feb 6 09:09:09 myserver postfix/smtp[20505]: 6BD362269D: to=<me@noone>, orig_to=<[email protected]>, 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 [email protected] [email protected] -- [email protected]

And the logs: Feb 6 09:17:11 myserver postfix/qmgr[28875]: E3B972264C: from=<[email protected]>, size=459, nrcpt=2 (queue active) Feb 6 09:17:11 myserver postfix/smtp[21559]: E3B972264C: to=<[email protected]>, orig_to=<[email protected]>, 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.

Solution 12 - Linux

This works on Centos7

echo "This is the main body of the mail" | mail -s "Subject of the Email" -r [email protected] [email protected]

Solution 13 - Linux

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 <[email protected]>" -s "test cli email $(date)" -- [email protected]

Hope this helps!

Solution 14 - Linux

For CentOS here is the working command :

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

Solution 15 - Linux

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

Solution 16 - Linux

I faced the same issue. But i solved the problem just be setting geko field in /ect/passwd field. Postfix by default send the mail from user login. Lets suppose you want to change from field to Alert. You just need to edit /ect/passwd file in any editor you like.

  1. vim /ect/passwd

root:x:0:0:Alerts:/root:/bin/bash

  1. Restart postfix.

Now check the result.

  1. echo "This is the main body of the mail" | mail -s "Subject of the Email" [email protected]

Now in recipient. From will be shown as Alerts as you have specified in geko field. Hope this solution works for you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoel CunninghamView Question on Stackoverflow
Solution 1 - LinuxdanielsView Answer on Stackoverflow
Solution 2 - LinuxPaolo BergantinoView Answer on Stackoverflow
Solution 3 - LinuxbfabryView Answer on Stackoverflow
Solution 4 - LinuxG.JView Answer on Stackoverflow
Solution 5 - LinuxLe DroidView Answer on Stackoverflow
Solution 6 - LinuxGeraldView Answer on Stackoverflow
Solution 7 - LinuxHardusView Answer on Stackoverflow
Solution 8 - LinuxVinko VrsalovicView Answer on Stackoverflow
Solution 9 - Linuxfun_vitView Answer on Stackoverflow
Solution 10 - LinuxFederico CassinelliView Answer on Stackoverflow
Solution 11 - LinuxJeradView Answer on Stackoverflow
Solution 12 - LinuxSantiago TriasView Answer on Stackoverflow
Solution 13 - Linuxscrat.squirrelView Answer on Stackoverflow
Solution 14 - LinuxYoussef ASEBRIYView Answer on Stackoverflow
Solution 15 - LinuxayonView Answer on Stackoverflow
Solution 16 - LinuxAttiq Ur RehmanView Answer on Stackoverflow