Today i’m going to address a question that comes up again and again on the various Linux help forums and mailing lists (in one form or another) :
« How do i set the From address when using mail ? »
Of course, « mail » in this case refers to that most basic of all Linux mail applications : GNU mail. Commonly part of the default software on a Linux box, it’s not used very often in an interactive capacity, but it still gets a lot of play in the System Administration world as a way to quickly fire off automated emails from scripts. For example :
$ echo "This is the message" | mail -s "This is the subject" email@example.com
This would result in a message that’s from whatever raw user@system that the local mailing software detects, such as « firstname.lastname@example.org » or some such thing. Normally, whatever defaults (including the From address) that get used are good enough, but occasionally it can be handy to have a finer level of control ; for example, policy restrictions on your local mail relay, or just because the default From address looks bad and you want to make it more readable.
The solution, like so many others in the Linux world, is straightforward once you already know about it, but just obscure enough that it isn’t obvious at first. The man page for « mail » on Ubuntu (and, likely, other distros as well) has this tasty little morsel of information listed as the very first option :
-a, --append=HEADER: VALUE Append given header to the message being sent
« Headers » form part of every email message, and are used to store all sorts of information about the email, from such pedestrian items as « From: » and « To: », to more esoteric things like Spam analysis breakdowns and binary encoding methods. For now, the important thing to realise is that the From: address is contained within the From header, and using the argument noted above, we can set it quite easily :
$ echo "The message" | mail -s "The subject" --append=FROM:email@example.com firstname.lastname@example.org
This would send the same message as above, but with the From: address set to « email@example.com ». In fact, any header can be altered in this fashion, not just the From header – so if you ever have the need to change other headers, now you know at least one way. 🙂
13 thoughts on “setting the from address in GNU mail”
This was just what I needed, thanks. On some mail programs, you have to use ‘-a “FROM: firstname.lastname@example.org‘” instead of the one above.
Have a nice day!
Excellent leads for me. Thank you both!
Turned out to get the from header (I use postfix) I needed to type:
mail -s “testing” ~h
Whoops! angle brackets got removed…
Glad that you found the article useful !
you can try this..it will work for you
mail -s “subject” email@example.com — -f firstname.lastname@example.org < /home/messagebody
I am using mail on CentOS 5x which is a variant of Fedora. The mail utility provided does not offer a ‘-a’ or a ‘–append’ option. Does anyone know how to specify a “from” address for this mail utility?
Got an answer to my own question: with the mail utility on CentOS, there’s no option to the mail command itself that lets you set the sender, however, If you imbed your call to the mail utility in a shell script, you can modify the USER and HOST environment variables to equal the email sender user and host you want and then export these. Here’s an example:
echo “This is a test.” | mail -s “A test…” email@example.com
Glad to see that you got it to work – thanks for the follow-up !
i want to send a mail to list of customers, based on their responses (if they are OK with changes) i want to perform some tasks, other wise i should stop executing those tasks, how can i perform this in a single script file
I don’t think you can do that with a single script file. You may wish to look at some proper mailing list management software, such as MailMan.
Thanks – very helpful.
You’re very welcome!
Great, very helpful!! exactly what I wanted! Thanx