Open source software security

Email on Raspberry Pi

5 October 2016

More than likely you're going to want your Raspberry Pi to send you e-mail for alerts and events. Raspbian comes with the exim4 mail transport agent (MTA) that you can use to send e-mail. Your ISP is probably blocking your ability to relay mail, but with a little work you can configure exim4 to relay through an external provider, such as Gmail. To begin, make sure that exim4 is installed with the command:

$ sudo apt-get install exim4 exim4-config

Once installed go ahead and start the exim4 configuration with the command:

$ sudo dpkg-reconfigure exim4-config

This will launch a semi-graphical interface, called curses, that will guide you through the configuration. You can use the tab key to navigate between options and the Enter key to select them.

On the first screen, Mail Server configuration, select 'mail sent by smarthost; no local mail.' as the option and click Ok. Next enter your Mail server configuration. You can use a made up name here, but it must be one that resolves in DNS or exim4 will fail. See documentation on dnsmasq to set up a local DNS server to resolve domain names. I used 'raspberrypi.madirish.net' for my domain name. Once you've filled in the name click 'Ok' to continue.

For the next screen enter the IP addresses you wish to relay mail for. Using the default of 127.0.0.1 (the reserved IP for localhost) is normally sufficient. Click Ok to continue.

Next enter domains that you wish to relay locally, meaning domains that the server should handle itself. Localhost or other local domains are fine. Enter whatever local domains you want and click 'Ok' to continue. The next screen asks what domain to append to usernames in From address lines for mail originating from the Raspberry Pi. It's a good idea to use the same name here as you did for the first mail configuration screen.

The next screen asks about the IP address or host name to use as a smart host. This is where you'll want to enter 'smtp.gmail.com::587' or another external provider. You enter the domain name of the mail relay, two colons, then the alternate port to use (the default port is 25, which is blocked by most ISP's).

Leave the list of recipient domains for which your server will relay mail blank and click 'Ok.' Similarly, unless you want to relay for other IP's on your LAN leave the IP ranges for unconditional mail relay blank and click 'Ok.'

Select 'No' when asked about keeping minimal DNS queries (Dial-on-Demand) and click 'Ok.' Finally select 'Yes' when asked about splitting configuration into smaller files. This will complete the configuration.

The only things left to do are to enter your credentials into a special file. To do this use vi, or another text editor, and edit the file /etc/exim4/passwd.client and add the line:

*.google.com:your_account_name@gamil.com:your_password

substituting in your gmail e-mail address and password so that you can authenticate. Once this is done you should restart exim with the command:

$ sudo systemctl restart exim4

And finally test your e-mail with the mail command, noting that entering a period on a single line ends the body of the mail. You can do so like this:

$ mail -s Test justin@madirish.net
Test
.
Cc: 

If you have any problems check your mail logs with the commands:

$ sudo tail /var/log/exim4/mainlog

Having e-mail relay set up should allow you to do all sorts of additional projects on your Pi including setting up log monitoring, security tools, and other packages that rely on e-mail for alerting.