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 firstname.lastname@example.org password yourpasswordhere from email@example.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="firstname.lastname@example.org" 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.
$ 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 email@example.com using the syntax shown:
$ mailx -s "O HAI SUBJECT LINE" firstname.lastname@example.org < /tmp/email_body.txt