How to send email from Ubuntu command line – using Gmail

This short guide will allow you to have your NAS send out email notifications with one command on the command line. You can then integrate this into various scripts etc to notify you when events happen (e.g. back script completes). To do this you’ll need a Gmail account (use an existing one or make a new one specifically for the NAS) to send the notification emails.


First install dependencies and open ssmtp.conf to edit:

Comment out original lines and add this to bottom – replacing the relevant lines with your own details. Note that the ‘AuthPass’ line should be completed using the “application specific passwords” which Google lets you create (https://support.google.com/accounts/answer/185833?hl=en ).

Send a test email using the sendmail command. What we did above effectively replaced the sendmail application with ssmtp. Now when you run sendmail it will point to the ssmtp command instead.

You can keep using sendmail/ssmtp but mutt is actually much more usable and convenient so lets install that. It allows easier sending of emails from the command line with more use friendly options and easier switches for attaching files etc. We already installed it above so let’s configure its use.
I usually turn off the function which saves your sent mail locally as this can, over time, fill up your disk. Let’s also change the ‘from’ address for sent mail,  nano /etc/.muttrc add this to the to:

Let’s test our email sending command now. First, let’s make a test file for email body contents:

Make a file to test sending attachments:

Send a test email – no attachment:

Send test email with attachment:

 

Useful sources
http://zackreed.me/articles/39-send-system-email-with-gmail-and-ssmtp
http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html
http://tombuntu.com/index.php/2008/10/21/sending-email-from-your-system-with-ssmtp/

Leave a Reply

(email optional)