Before we can sent email form the Raspberry Pi through our Gmail account, we will need to install a message transfer agent (MTA). A simple and lightweight package is Simple SMTP.
To install ssmtp:
Before installing software, you should update your package list:
sudo apt-get update
Next we can install the ssmtp package:
sudo apt-get install ssmtp mailutils
To configure ssmtp:
Now that the ssmtp package is installed, we need to open the configuration file in an editor and set it up to use Gmail.
sudo nano /etc/ssmtp/ssmtp.conf
At the bottom of the configuration file, add the folowing lines:
# Config file for sSMTP sendmail
# ... add the following ...
Importaint: there should be no spaces between the ‘=’ and your settings!
To test the installation of ssmtp:
To verify that we have configured ssmtp correctly to send emails, run this simple command to send a test message to the root email address that we configured in configuration file.
echo "This is a test..." | mail -s "Test Message" YOUREMAILADDR@YOURPROVIDER
You can call sSMTP directly with the verbose option (-v) to allow us to see the communications as the message is being sent.
printf "Subject: %s\n\nThis is a test..." "Test Message" | ssmtp -v root
To view all ssmtp log enties:
sudo grep -riI ssmtp /var/log > /tmp/ssmtp.log && cat /tmp/ssmtp.log
Once Simple SMTP is installed and configured properly, it becomes the default program on the Raspberry Pi to forward all email messages.
Optional, how to send email messages with attachments:
It is also possible to send email messages with attachments. Another simple package is mpack – pack a file in MIME format.
To install mpack:
Install the mpack package:
sudo apt-get install mpack
To test the installation of mpack:
To test that we can send email attachments, run this simple command to send a test message to the root email address with a temporary file.
ls -l >/tmp/ls.txt && mpack -s "Test Attachment Message" /tmp/ls.txt root && rm /tmp/ls.txt
For more information on options for ssmtp and mpack, see the man pages.