debian raspbian lubuntu ssmtp gmail – Simplest Way to Send One-Line Mail via Command Line Using Gmail?

debiangmaillubunturaspbianssmtp

Using Raspbian and Ubunntu 16.04 LTS so need a generic Linux solution.

Requirement is simple:

I need a way to send one-line email messages from the command line.

I have set up a gmail account just for this particular Rpi3, with the address of rpi3abc@gmail.com – with no 2FA

So now I need to be able to send one-line mail messages from anywhere (including cron) without user intervention.


I also would like it to be able to send text files; basically, anything from stdin.

Best Answer


The simplest answer to sending one-line messages via gmail is to use ssmtp


Install it with the following commands:

sudo apt-get update
sudo apt-get install ssmtp

Edit /etc/ssmtp/ssmtp.conf to look like this:

root=rpi3abc@gmail.com
mailhub=smtp.gmail.com:465
FromLineOverride=YES
AuthUser=rpi3abc@gmail.com
AuthPass=testing123
UseTLS=YES

Send a one-liner like so:

echo "Testing...1...2...3" | ssmtp myusername@gmail.com

or

printf "Subject: Test\n\nTesting...1...2...3" | ssmtp myusername@gmail.com

Then, true to *nix, you just get the prompt back in a few seconds.

Check your myusername@gmail.com account, and voila, it is there!


This also works well when sending a file, as so:

cat program.py | ssmtp myotherusername@yahoo.com

And the program will show up in the mailbox

If the file is a text file, it can have a first line that says Subject: xxxxxx

This can be used with various cron jobs can send me data with subject lines indicating the content.


This will work with anything that prepares a message that is piped into ssmtp via stdin.


For more details such as securing these files against other users and such, visit this article:

Send Email from Raspberry Pi Command Line


Be sure to also look down below to the answer posted by Rui about locking down the FROM: address that might be changed in formatted message files, if necessary.


Now if only I could figure out how to send SMS the same way.

Related Question