I’m working on a project where I need to send email from my Raspberry Pi. Installing a full-blown SMTP server would be overkill, I just need something where I can send messages from a bash script.
A brief search led me to a forum post from 2013 which talked about configuring the ssmtp package. That post in turn referenced a step-by-step guide from 2009. Unfortunately, both seem to be out of date, and the latter is for installing it on CentOS?RHEL/RedHat/Fedora. So here’s my attempt at an updated version for the Pi (which should apply to any Debian-based Linux distribution).
- These instructions send via Gmail. If you’re using two-factor authentication (and you really should), you’ll need to set up an application -specific password. Otherwise, you’ll get authentication errors.
- The password is stored in plain text. This solution is not suitable for use on a shared system.
sudo apt update -y && sudo apt upgrade -y sudo apt install -y ssmtp sudo vi /etc/ssmtp/ssmtp.conf
Make these changes to the
mailhub=smtp.gmail.com:463 FromLineOverride=YES AuthUser=Your_GMail_Address AuthPass=Your_GMail_Password UseTLS=YES
I also set the
root= setting to my email address. I don’t believe this is necessary, but it does allow me to get notified when something goes wrong with one of my messages. (The way I first found out my configuration was working was a message from a cron job which had some unexpected output.)
Part of the installation is to set up a symlink so that
sendmail becomes an alias for
ssmtp. You can use either command.
ssmtp command doesn’t seem to include command-line options for specifying the subject line or the name of the recipient..
So, here’s a command line you can use. Edit the email address as suits your needs. (The sender name and email address will be embedded by GMail.)
Ignore the word-wrap, this is all one line.
echo -e "Subject: Test Message\nTo: Your Name Here <email@example.com>\nThis message was sent via ssmtp." | ssmtp -t
Alternatively, you can put the recipient’s email address on the command line (the message will then be received as a BCC).
echo -e "Subject: Test Message\nThis message was sent via ssmtp." | ssmtp firstname.lastname@example.org
Four files are written to
- mail.err – contains an entry for each time there’s a problem sending a message.
- mail.info – contains an entry for each attempt (successful or failed) at sending a message
- mail.log – duplicates mail.info.
- mail.warn – duplicates mail.err.
(Image via Pixabay user Deans_icons used under Pixabay License.)