lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
Ubuntu: Command Line Email
For a simpler, updated version of this guide, please see: http://klenwell.com/is/UbuntuCommandLineGmail


problem

I want to send email using the command line on my laptop Ubuntu system. Mainly for cron jobs and automated backups. I want to be able to do so without having to set up a full-fledged MTA like sendmail or exim. And I want to be able to use either my ISP email account or a Gmail account.

Seemed simple enough and it probably is for people who do this type of thing for a living. Took me all night. So hopefully this will save someone else (perhaps me again in the future) hours of unnecessary frustration.

solution

First, look at this diagram from Wikipedia. Without being able to find a simple step-by-step tutorial to guide me, the biggest problem I was having was sorting out what was my MUA, what was my MTA, and what if anything I needed to connect the two. Long story short, they are as follows:

MUA (the client): nail (you can also use mailx or mutt or even evolution)
MTA (the mail server): your isp or gmail
MSA (smtp middle man): msmtp (a simple MTA that gets mail from your client to your real MTA or mailhub)

step-by-step

Install the needed programs
$ sudo apt-get install msmtp
$ sudo apt-get install nail


Install Thawte certificate for Gmail
This is necessary (I think) for Gmail. Probably the most complicated step, though not too bad thanks to instructions here:
$ 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


Configure msmtp
Replace UPPERCASE text with your personal settings
$ gedit ~/.msmtprc

This will open up an msmtp configuration file where you'll want to copy the following lines, with your correct settings, of course:
# config options: http://msmtp.sourceforge.net/doc/msmtp.html#A-user-configuration-file
defaults
logfile /tmp/msmtp.log



# isp account
account isp
auth login
host SMTP.YOURISP.COM
port 25
user YOURNAME@ISP.COM
from YOURNAME@ISP.COM
password *****



# gmail account
account gmail
auth on
host smtp.gmail.com
port 587
user YOURNAME@gmail.com
password *****
from YOURNAME@gmail.com
tls on
tls_trust_file /home/USER/etc/.certs/ThawtePremiumServerCA.crt



# set default account to use (from above)
account default : isp


Change permission on this file or msmtp will complain:
$ chmod 600 ~/.msmtprc


Configure nail
$ gedit ~/.mailrc

The key line below is "set message-sendmail-extra-arguments". It seemed like I should have been able to add this to the set sendmail command. Suffice to say, not well documented.
# set smtp for nail
# ref: http://ubuntuforums.org/showpost.php?p=4531994&postcount=6
# docs: http://msmtp.sourceforge.net/doc/msmtp.html#Configuration-files

# isp account (default)
# $ nail -s "subject line" -a /path/file recipient@email.com < /path/body.txt
set from="YOURNAME@ISP.COM"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a isp"

# gmail account
# $ nail -A gmail -s "subject line" -a /path/file recipient@email.com < /path/body.txt
account gmail {
set from="YOURNAME@gmail.com (YOURNAME)"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"
}


Send test messages for both accounts

$ echo -e "testing email from the command line" > /tmp/test_email
$ nail -s "isp test" YOURNAME@gmail.com < /tmp/test_email
$ nail -A gmail -s "gmail test" YOURNAME@gmail.com < /tmp/test_email


Check your gmail account and you should have two new messages -- one from that account and one from your ISP account. To check your log:
$ gedit /tmp/msmtp.log


Conclusion

Only took me all Friday night to figure this out. But, hey, better than getting drunk, stoned, and laid. :)

I've added this post to ubuntuforums.org: http://ubuntuforums.org/showthread.php?t=780509

Labels:

Really great job, I have spent the last 2 days trying to figure out how to do this. I need to send email from a remote machine to keep track of its dynamic ip using wget from something like whatismyipaddress dot com. Thanks so much for this information, you saved me several weeks of poke and hope :P.

Mangler
You really got my started with using command line emails.

However, if your purpose is just to send some email with some attachment it is really not necessary to install both nail and msmtp.
One of them will suffice.

If you want to keep things simple I would go for just nail. You can just set the smtp-server in the config file (either ~/.mailrc or /etc/nail.rc) with set smtp=smtp.server.name.
Or you can specify it on the command line with -S smtp=smtp.server.name.

So in its most simple form
sudo apt-get install nail
Put your message body in a file called mymessage
nail -s "My subject" -a Myattachment -S smtp=smtp.server.name recipient@somedomain.com < mymessage

And use this in some shell-script.

At least this worked for me.

Joost
However, if your purpose is just to send some email with some attachment it is really not necessary to install both nail and msmtp.
One of them will suffice.


Thanks for the comment, Joost. It seems to me I went down that road, but found something missing.

Nevertheless, I'm for any simplification of the process. Will this allow you to use either a gmail or regular isp account?
A BIG thank you! This saves me a lot of time...
Marco - greetings from The Netherlands
Thanks a lot.. sendmail is a pain in the ass and nail just works!

Thanks a lot!
This comment has been removed by the author.
I had problems with the certificates from the provided url, since the zip archive's content is not as described anymore. Instead, I did:

cp /etc/ssl/certs/Thawte_Premium_Server_CA.pem /home/USER/etc/.certs/ThawtePremiumServerCA.crt

where USER is your user name.
Looks like it would have been really helpful, unfortunately nail has been dropped from the Ubuntu Hardy repositories, the best looking alternative appears to be msmtp which works a little like sendmail but with more configuration possible.

Thanks for your hard work!
just wanted to say that nail is available in the ubuntu repositories.

The name for the package is: "heirloom-mailx"

In Lucid, you can also "apt-get install nail", which is a dummy package for "heirloom-mailx"
Ubuntu Maverick (10.10)

Thanks for the info! I had to change the following:

Don't bother downloading the certs - I already have them.

Dont bother making the ~/etc/.certs directory - I already have them.

Change .msmtp so that tls_trust_file points to /etc/ssl/certs/ca-certificates.crt

Then you are good to go!
Thanks for writingg