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.
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.
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
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).
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
It may be necessary to run the next command to make the changes in aliases active:
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 aliasses 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.
Per 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.
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.
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" firstname.lastname@example.org to send an email with a file attached to it.
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
My way of keeping this site alive.