setting the from address in GNU mail

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" user@domain.tld

This would result in a message that’s from whatever raw user@system that the local mailing software detects, such as « » 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:sensible@domain.tld user@domain.tld

This would send the same message as above, but with the From: address set to « sensible@domain.tld ».  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. 🙂

Author: phrawzty

I have a computer.

13 thoughts on “setting the from address in GNU mail”

  1. 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
    FROM: ^D
    CC: ^D


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


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


    export USER
    export HOST

    echo “This is a test.” | mail -s “A test…” to.addressee@to.hostname


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


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


Leave a Reply to clayzer Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: