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.
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.
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@example.com
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.
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
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.
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" firstname.lastname@example.org < body.txt cat body.txt | mail -s "This is the subject line" email@example.com