Specify the from user when sending email using the mail command

Viewed 209245

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.

16 Answers

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

$mail -s "Some random subject" -a "From: some@mail.tld" to@mail.tld

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

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.

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

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!

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!

For CentOS here is the working command :

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