Setting the sender address in Ubuntu
Saturday, 30 August 2008 00:00

Sometimes, you may want to be able to send an e-mail from a shell script or manually from the command line. For example, you may want to periodically send out a server health e-mail. We found that when doing this, it is a little tough to control how the sender's e-mail address will appear. The first problem we ran into was that our domain was appearing as something like user@localhost.local. The second problem is that we didn't want to expose the user's name, which could be root or www. We were looking for a way to have the sender's address appear as This e-mail address is being protected from spambots. You need JavaScript enabled to view it (or something along those lines).

Step 1 - Install Postfix & Mailutils
In our environment, we had already installed Postfix to send outgoing mail. This can be done as follows:

sudo ant-get install postfix

You may need to step through some configurations to get it up and running. Once installed, you then need to install additional mail tools to be able to send email from the command line:

sudo apt-get install mailutils

Step 2 - The Easy Solution
Once installed, you can send e-mails from the command line like so:

echo Email body | mail -s "subject line" -a From: This e-mail address is being protected from spambots. You need JavaScript enabled to view it This e-mail address is being protected from spambots. You need JavaScript enabled to view it

This will send an e-mail to This e-mail address is being protected from spambots. You need JavaScript enabled to view it , and will use the address of This e-mail address is being protected from spambots. You need JavaScript enabled to view it .

We say this is the "easy solution" because, although it works, the recipient would still be able to look at the mail message headers and see the address of user@localhost.local. This simply sets the "From" header of the email, but the sender envelope still contains the original user's address.

Step 3 - Sender Aliases
We took this a step further to try to fully control the sender's e-mail address. There were two configurations we made; one to control the sender's domain, the other to apply an alias to our user name.

First, edit your Postfix configuration file as follows:

sudo nano /etc/postfix/main.cf

You must either change or add the following lines, using the domain you would like to show in the sender's address:

myhostname = domain.com
smtp_generic_maps = hash:/etc/postfix/generic

Next, you need to configure the alias file. This file maps users to the proper external name to use. You can also have a catch-all alias.

sudo nano /etc/postfix/generic

This e-mail address is being protected from spambots. You need JavaScript enabled to view it This e-mail address is being protected from spambots. You need JavaScript enabled to view it
This e-mail address is being protected from spambots. You need JavaScript enabled to view it This e-mail address is being protected from spambots. You need JavaScript enabled to view it
@domain.com This e-mail address is being protected from spambots. You need JavaScript enabled to view it

After this file is created, you'll need to use postmap to install it, then reboot Postfix:

sudo postmap /etc/postfix/generic
sudo /etc/init.d/postfix restart

After this is all done, you can send an email without needing to specify the From header, and it will still show as our noreply address:

echo Email body | mail -s "subject line" This e-mail address is being protected from spambots. You need JavaScript enabled to view it