Scriptable sendmail via Gmail in Ubuntu 8.04*

*There’s no reason this shouldn’t work in later versions.

The purpose of this setup is to configure Ubuntu Linux so that you can send an email with a single command from a bash terminal. The idea behind this is that once set up to do it, your computer can now actively alert you (or others) to state changes of your choosing (or not). Phosphorus and Lime deserves accolades for a great deal of this procedure. My omissions/simplifications assume that you are setting this up from scratch, and are only planning to use this through an existing gmail account.

Now then. You’re going to need a mail client and a SMTP handler. For these purposes, mailx and msmtp, respectively, will serve you well.

$ sudo apt-get install mailx msmtp

Google’s SMTP service (kindly provided free for gmail account holders) makes use of Thawte SSL certificates. Since /etc is only writeable by root (meaning “sudo sudo sudo sudo”), let’s dump and configure them in ~/ like so:

$ mkdir -p ~/.etc/.certs
$ chmod 0700 ~/.etc/.certs
$ cd ~/.etc/.certs
$ wget https://www.verisign.com/support/thawte-roots.zip --no-check-certificate
$ unzip thawte-roots.zip
$ cp Thawte\ Server\ Roots/ThawtePremiumServerCA_b64.txt ThawtePremiumServerCA.crt

msmtp reads a local config file ( ~/.msmtprc ), containing several important directives, at runtime. Open a text editor and create it:

$ nano ~/.msmtprc

…then enter the following (customized to your account):

account gmail
auth on
host smtp.gmail.com
port 587
# your email address and password on the next 3 lines...
user yourgmailaccount@gmail.com
password yourpasswordhere
from yourgmailaccount@gmail.com
tls on
tls_starttls on
# tls_trust_file argument is the full path to the certificate
# "myusername" is, uh, your user name
tls_trust_file /home/myusername/.etc/.certs/ThawtePremiumServerCA.crt
maildomain gmail.com
account default : gmail

Save and exit (or in nano’s case, exit and save) your editor, then give only yourself read/write permissions to the file:

code>$ chmod 600 ~/.msmtprc

Now we configure some runtime parameters for mailx:

$ nano ~/.mailrc

Add the following lines, customized to your gmail account:

set from="yourgmailaccount@gmail.com"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"

Make sure you’ve opened port 587 to the outside world from your computer through your router!

Save/exit. If you’re so inclined, you can create a preformatted plaintext file which will serve as the body of the email. As you can imagine, this opens up all sorts of options to you.

Example:

$ echo "Here's some stuff that will appear in the body of the email." > /tmp/email_body.txt
$ echo -n "Sent on " >> /tmp/email_body.txt
$ date >> /tmp/email_body.txt

You should be able to fire off an email to example@gmail.com using the syntax shown:

$ mailx -s "O HAI SUBJECT LINE" example@gmail.com < /tmp/email_body.txt

MAGIC 8O

November 11, 2008 • Posted in: Linux

3 Responses to “Scriptable sendmail via Gmail in Ubuntu 8.04*”

  1. Andrew - December 11th, 2008

    THANK YOU!!! I’ve been trying to do this for a few days and have went through a few other much more complicated tutorials with no avail. very nice

  2. Andrew Strong - January 9th, 2009

    Great page! Have you tried simply writing the message in the Terminal? vim will usually be invoked for this, simply use:

    $ mailx email.address@whatever.com

    and press enter, you will be prompted for a Subject. Press enter and type the message, dividing the lines with enter. At the end of your message, press -D at the start of a new line and the message is sent. Cool?

  3. Andrew Strong - January 9th, 2009

    Doh!! The command to send the message was mangled by the blog software: should be control D .

Leave a Reply

You must be logged in to post a comment.