Prepare Your Pi To Send Mail Through Gmail

Suppose you have set up your Raspberry Pi to do some standalone work. It would be nice if it could email you occasionally, for instance if there's something wrong. Or it may send you status updates on the work it's doing. For this you could use the SMTP server of your ISP. However, if your Pi isn't stationary and roams around on multiple networks, this is not an ideal situation. If you connect your Pi to a different network, operated by a different ISP, chances are that you can't send any messages.
A better solution would be to use a Gmail account for that. In this description I presume you already have a Gmail account. If not, you can get one for free at www.gmail.com. You may also decide to create a new one for your Raspberry Pi. The same account can even be shared among multiple Raspberry Pies. Rumour has it though that Gmail will only allow 100 mails sent per account per day. So if you share your account among too many machines you may start hitting the ceiling soon.

Because your account's password will be readable by the root user on your Raspberry Pi, I suggest to setup two step authentication in your Gmail account. That way you can create an application specific password for your Raspberry Pi. With such a password it's not possible to access your account settings, and it can easily be discarded should it get compromised, without jeopardizing the rest of your email account.

Getting A Message Transfer Agent

Before your Pi can send emails it needs a message transfer agent (MTA). I have two flavours for you. The first one is exim4, the other one is SSMTP. The choice is yours. Needless to say, you will only need one of the two MTAs.
Thanks to the Debian packaging system this is easily done by typing one of the following commands.

Exim4

sudo apt-get install exim4

After installing exim4 we need to configure it. This is done by the following command:

sudo dpkg-reconfigure exim4-config

Now you need to answer some questions. Don't worry I'll give you the answers to those questions.

  • The first screen asks you what type of mail server you need. Select the second option: "mail sent by smarthost; received via SMTP or fetchmail"
  • The next question asks for the system mail name: Set to same as hostname (raspberrypi)
  • Now it asks you what IP addresses should be allowed to use the server. Leave as is (127.0.0.1 ; ::1)
  • Other destinations for which mail is accepted: raspberrypi
  • Machines to relay mail for: Leave blank.
  • IP address or host name of outgoing smarthost: Replace by: smtp.gmail.com::587
  • Hide local mail name in outgoing mail: Select: No
  • Keep number of DNS-queries minimal: Select: No
  • Delivery method for local mail: Select: "Maildir format in home directory"
  • Split configuration into small files: Select: No
  • Root and postmaster mail recipient: Enter: pi

After answering all these questions exim4 will restart and we're halfway home.

Now you'll have to enter your account details. As root, edit the file /etc/exim4/passwd.client and add the next three lines at the end of the file.

gmail-smtp.l.google.com:[email protected]:PASSWORD
*.google.com:[email protected]:PASSWORD
smtp.gmail.com:[email protected]:PASSWORD

Needless to say that you'll have to change YOU to your Gmail login name, and PASSWORD to your password on all three lines. After that you only have to update and restart exim4 and you're done! The next two lines will do that for you:

sudo update-exim4.conf
sudo service exim4 restart

SSMTP

sudo apt-get install ssmtp mailutils mpack

Now edit the file /etc/ssmtp/ssmtp.conf as root and add the next lines. Please note that some of the lines already exist and may need to be changed. Others don't exist yet and need to be added to the end of the file.

mailhub=smtp.gmail.com:587
hostname=ENTER YOUR RPI'S HOST NAME HERE
[email protected]
AuthPass=PASSWORD
useSTARTTLS=YES

Again you'll have to replace YOU with your gmail login name and PASSWORD with your (application specific) gmail password.
After this you're done. You don't even have to restart the SSMTP server (in fact, there is none).

The Final Touches

Some processes, for instance crontabs, can send mails to root or other system users. If you don't want to miss any of them you can setup some aliases. You can do that by editing the file /etc/aliases. Here's what mine looks like:

# /etc/aliases
mailer-daemon: postmaster
postmaster: root
nobody: root
hostmaster: root
usenet: root
news: root
webmaster: root
www: root
ftp: root
abuse: root
noc: root
security: root
root: pi
pi: [email protected]

It may be necessary to run the next command to make the changes in aliases active:

sudo newaliases

This tells the system to redirect all mail to root, while mail to root is redirected to the user pi, while mail to user pi is finally redirected to my own mail account. This way all system mail will eventually be sent to my own mail account, no matter to what local user the mail was originally sent.

If you are like me, you will probably have more than one Raspberry Pi mailing you some status information. And unless you use different mail accounts for all your RPis it becomes harder and harder to find out which one is mailing you. The next command will setup a new full name (pi @ domotics) for the user name pi, with which you can idintify the source of the e-mail.

sudo chfn -f "pi @ domotics" pi

PS: The aliases don't appear to work with the SSMTP setup. Nothing I tried seemed to work. Although SSMTP is easier to setup, I recommend you to use the Exim4 approach to get a more complete mail experience.
But if it is simplicity you're after, feel free to use SSMTP.

Testing

To test your outgoing mail simply execute the next command:

mail -s "This is the subject line" root@localhost

Then type the body of the message, this is only a test so anything will do. When you're done typing the body type a dot (.) at the beginning of a new line and hit Enter. The mail should now be sent to root, which is redirected to pi, which is redirected to your normal email address.
You can also send a mail directly to your own email address if you want to, however that would be skipping testing the redirections.

Below you see two other ways of sending mail from your RPi. Both methods will send the file body.txt as message body.

mail -s "This is the subject line" [email protected] < body.txt
cat body.txt | mail -s "This is the subject line" [email protected]