Simplest way to send one-line mail out via command line using gmail?


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:

[email protected]
mailhub=smtp.gmail.com:465
FromLineOverride=YES
[email protected]
AuthPass=testing123
UseTLS=YES

Send a one-liner like so:

echo "Testing...1...2...3" | ssmtp [email protected]

or

printf "Subject: Test\n\nTesting...1...2...3" | ssmtp [email protected]

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

Check your [email protected] account, and voila, it is there!


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

cat program.py | ssmtp [email protected]

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.


ssmtp is just one of many Sendmail wrappers. All of these accept a message on standard input, and optionally a list of addresses as command-line arguments, and they all offer a binary named sendmail which implements (at least the basic features of) the traditional Sendmail command-line API. But properly speaking, that message needs to be well-formed RFC822 message. At minimum, it should have a Subject: header.

ssmtp [email protected] <<<$'Subject: testing 1...2...3'

(With ssmtp, sendmail is just a symlink to ssmtp. Postfix, Exim, and I believe every other MTA which Provides: mail-transport-agent has a similar arrangement, except of course sendmail where the sendmail binary is "the real thing".)

More commonly, you can piece together a simple email message with a here document.

/usr/lib/sendmail -oi -t <<____HERE
Subject: testing
To: [email protected]

Here we interpolate the shell variable $result
____HERE

(The Sendmail -t option says to take the recipient list from the headers of the message you receive on standard input. The precise path to Sendmail will differ between platforms.)

Another common variation is to combine the output of a few commands. Take care to have an empty line (a "neck") between the headers and the message body.

( printf "Subject: random number\n\n"
  dd if=/dev/urandom bs=4 count=1 2>/dev/null | od -D -An ) |
sendmail [email protected]

For anything beyond very simple ASCII text-only messages, you need to understand how to compose a proper MIME message, at which point it usually makes more sense to use a tool like mutt. Some platforms have a mail or mailx which knows how to send attachments and non-ASCII text, but this is not fully portable.

The challenge here is not finding a client which can take an email message and attempt to send it, it is to configure it for the specifics of Gmail, which requires the MTA to know the user name and password to use for the remote server in order to be able to use it as the outgoing smarthost.

Behind the scenes, most clients like mutt, mailx, etc typically just run sendmail behind the scenes to get the message off the system.


Adding to the OP own answer:

When configuring ssmtp, you may also forbid or allow users from defining the From, and also override the domain; you might want to do that for several reasons including the message not falling in the Spam folder.

You can add to /etc/ssmtp/ssmtp.conf:

# Where will the mail seem to come from?
rewriteDomain=my_internet_domain.uk

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

Please note that while YES can be used in a home raspberry, it might not be advisable in a multi-user system from the security point of view.