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 « systemcheck@2.1.0.192.int.domain.tld » 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. 🙂
This was just what I needed, thanks. On some mail programs, you have to use ‘-a “FROM: mymail@mail.tld‘” instead of the one above.
Have a nice day!
LikeLike
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
LikeLike
Whoops! angle brackets got removed…
LikeLike
Glad that you found the article useful !
LikeLike
hi,
you can try this..it will work for you
mail -s “subject” recive@domain.com — -f sender@domain.tld < /home/messagebody
thanks,
Raja K
LikeLike
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?
LikeLike
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:
#!/bin/bash
USER=your.sender.name
HOST=your.sender.host
export USER
export HOST
echo “This is a test.” | mail -s “A test…” to.addressee@to.hostname
LikeLike
Glad to see that you got it to work – thanks for the follow-up !
LikeLike
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
LikeLike
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.
LikeLike
Thanks – very helpful.
LikeLike
You’re very welcome!
LikeLike
Great, very helpful!! exactly what I wanted! Thanx
LikeLike