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 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 to 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.
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:
- Machines to relay mail for:
- IP address or host name of outgoing smarthost:
- Hide local mail name in outgoing mail:
- Keep number of DNS-queries minimal:
- Delivery method for local mail:
Select: "Maildir format in home directory"
- Split configuration into small files:
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.
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 exim and you're done!
The next two lines will do that for you:
sudo /etc/init.d/exim4 restart
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.
hostname=ENTER YOUR RPI'S HOST NAME HERE
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 Touch
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:
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 mail will eventually be sent to my own mail account, no matter to what local user the mail was originally sent.
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 normal email 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.
mail -s "This is the subject line" email@example.com < body.txt
cat body.txt | mail -s "This is the subject line" firstname.lastname@example.org