Monday, 05 October 2015 13:43

Send Email from Raspberry Pi Command Line

Written by 
Rate this item
(4 votes)

Send email using SSMTP with the command line on a Raspberry Pi

Step-by-Step Illustrated guide to sending emails through the command line in linux

I needed my Raspberry Pi to notify me it's local and WAN IP address once it was connected to the network.  I found using email was the easiest and most reliable way.

The raspberry pi I'm using, will be installed in different network environments, and I needed a reliable way to know what the current IP address of my pi was.  Using ssmtp, gmail, and a shell script this can easily be accomplished.  This article wil go over the steps I took to to set up the email program.  This setup will work in a variety of linux environments, this example is using a raspberry pi running raspbian.


Setup your Raspberry Pi

You need to have an image loaded and running on the Pi.  I used raspbian in this example.  You can follow these steps to get all of the basic settings configured: Raspberry Pi setup without a Display


Setup a Gmail Account to work with ssmtp

You can easily use your own ISP's smtp host if allowed, but you'll need to know the ports they use, if they allow without authentication, etc.  We can use the same setup consistently, instead of figuring out what each ISP allows.

Create a new Gmail account.  You'll have to allow access to less secure apps, in your profile in order for this to work, which is a little tricky to find.

When you log into your gmail account, Goto Settings > Accounts and Import  then find Other Google Account settings

Send Email Raspberry Pi 03


Then goto Personal info & privacy > Account overview

Send Email Raspberry Pi 05 


Then, on the following page goto Sign-in & security > Connect apps & sites, select to turn on Allow less secure apps from OFF to ON

Send Email Raspberry Pi 04 


Install and configure the ssmtp program

This is not a full on email server, but it's perfect for sending short, quick status emails through the command line or variety of scripts utilizing gmail.

Install ssmtp

sudo apt-get update
sudo apt-get install ssmtp

Backup the original configuration file

sudo mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.bak

Create a new configuration file (hit ctrl-x and then y to save the file)

sudo nano /etc/ssmtp/ssmtp.conf

Here is my sample ssmtp.conf file (change the 2 lines with <gmail-user> to your gmail account name, change line <gmail-password> to your gmail password)



# Debug=Yes

Send Email Raspberry Pi 01

Secure the ssmtp configuration file

The gmail user and password information is stored in /etc/ssmtp/ssmtp.conf, which any user can read.  For a little additional security, we'll change so only ssmtp and root can read the file.

Add the group ssmtp

sudo groupadd ssmtp

Set /etc/ssmtp/ssmtp.conf group owner to the new ssmtp group you just created

sudo chown :ssmtp /etc/ssmtp/ssmtp.conf

Set the group owner of the ssmtp binary to the new group.  In debian distributions the ssmtp binary is located in /usr/sbin/ssmtp.  If you recieve an error chown: cannot access ‘/usr/sbin/ssmtp’ you need to locate the binary ssmtp file sudo find / -name "ssmtp" and use that path instead. (such as /usr/bin/ssmtp)

sudo chown :ssmtp /usr/sbin/ssmtp

Make sure only root, and the ssmtp group can access /etc/ssmtp/ssmtp.conf

sudo chmod 640 /etc/ssmtp/ssmtp.conf

Set the SGID bit on the ssmtp binary.  Find replace the path /usr/sbin/ssmtp if in a different location on your distribution as shown above.

sudo chmod g+s /usr/sbin/ssmtp


Send an email from the command line

Send a test email (replace <to-email-address> with the recipient’s email address you want to send a message to ie: This email address is being protected from spambots. You need JavaScript enabled to view it.)

echo "This is a test" | ssmtp <to-email-address>

Send a more detailed email

printf "To: <to-email-address>\nFrom: VoIP Server\nSubject: Testing\n\nThis is a test\n" | ssmtp -t
  • ssmtp -t - tells ssmtp to read message (via the | pipe), searching for recipients. ''To:'', 'Cc:'', and ''Bcc:'' lines will be scanned for people to send to. Any addresses in the argument list will be suppressed.
  • \n - creates a carriage return, notice the required double carrage return between the Subject line and Body


Here's is what it looked when I received it in at yahoo

Send Email Raspberry Pi 02


Working PC:

OS cat /etc/*release

Ubuntu 14.04.3 LTS

Kernel uname -mrs

Linux 3.13.0-62-generic x86_64


Raspberry Pi (model B):


Raspbian GNU/Linux 8 (jessie)


Linux 4.1.10+ armv6l



Read 32385 times Last modified on Monday, 08 February 2016 14:16
Algis Salys

Creator and owner of  Linux enthusiast, electronics tinkerer, and likes to spend time in the workshop building and creating new projects.