5 – Configuring Email on the RPi

Prepare Your Pi To Send Mail Through Gmail

Suppose you have set up your Raspberry Pi to do some stand alone 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 RPi isn’t stationary and roams around on multiple networks, this is not an ideal situation. If you connect your RPi to a different network, operated by a different ISP, chances are that you can’t send any messages.

UPDATE: This procedure also works with any Linux distribution.

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 Pis. Rumor 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.

A Hostname is a way to identify your RPi. Although Hostnames are not a unique key, you should name each of your network devices something unique. I’d suggest coming up with a naming convention of some sort. It could be as simple as YourCallsign_001 and just use a sequence. Other popular methods include using the names of planets, Gods from Greek Mythology, Cities, characters from movies or TV. It’s really up to you, but be sure to name each of your RPis something different since the email that you will receive from your RPi will bare the Hostname so you know where it came from.

I strongly recommend that you not use your personal email address. For example, you may want am account such as YourCallsign@gmail.com. Although it may certainly be convenient to receive email to this address, it can become cumbersome, even with the use of filters. So, consider creating another account YouCallsign.RPI@gmail.com. I do not recommend creating an account for each RPi you deploy unless you expect it to be a high email utilizer.

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 to flavors 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. My preference in exim4, but we’ll cover both.

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: Enter: 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

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:YOU@gmail.com:PASSWORD
*.google.com:YOU@gmail.com:PASSWORD
smtp.gmail.com:YOU@gmail.com: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 /etc/init.d/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
AuthUser=YOU@gmail.com
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
webmin: root
root: pi
pi: youremail@example.com

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 @ hostname) for the user name pi, with which you can identify the source of the e-mail. “hostname” is the Hostname.

sudo chfn -f "pi @ hostname" pi

The aliasses don’t appear to work with the SSMTP setup. Although SSMTP is easier to setup, I recommend you use the Exim4 approach to get a more complete mail experience.

But if it is simplicity you’re after, feel free to use SSMTP.

UPDATE: Google has a security feature that will not allow “unapproved or insecure applications” access to GMail to send or receive email. They do provide a way around this though. Go to https://myaccount.google.com. Look for the section “Sign-in & Security”. There is a link “Connected Apps & Sites”, click that link. Scroll down to “Allow less secure apps”. By default this is set to “On”; change it to “Off”; I know it sounds backwards. Ok…you are all set. Continue on to the next section.


A Better Mail User Agent

By default the program bsd-mailx is installed on the Raspberry Pi as the mail user agent. Every time you invoke the mail command, bsd-mailx is executed; this includes mail being sent from perl or php.

There’s one thing that I don’t like about this program and that it won’t allow you to send attachments with your mails.

Update: If you are using a regular Linux distribution on a regular PC (non-RPi), then skip to the testing. You may not need to worry about changing or adding an agent.

So let’s remove that program and install a better alternative:

sudo apt-get purge mbsd-mailx
sudo apt-get install heirloom-mailx

After that you can simply do something like…

mail -a attachment.file -s “Subject line” to@example.com

…to send an email with a file attached to it.


Testing

To test your out going 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 gmail address.

You can also send a mail directly to your own email address if you want to, however that would have skipped the redirections from the test.

Below you see two other ways of sending mail from your RPi. Both methods will send the file body.txt as message body. You could use these methods as a crontab to send you a weekly log file, or some other data file. It’s really up to you…get creative.

mail -s "This is the subject line" someone@example.com < body.txt
cat body.txt | mail -s "This is the subject line" someone@example.com