Command Line Email on Ubuntu (nail version)

For a simpler, updated version of this guide, see this page: http://klenwell.com/is/UbuntuCommandLineGmailNail

This will be the first of two posts showing you how to configure the more recent versions of Ubuntu to send email from the command line as simply as humanly possible using your Gmail account for delivery. These instructions were originally posted on my old blog and on the Ubuntu forums site.

This guide covers using the nail program, which is my client of choice but is no longer in the main repositories. Find a guide for using mailx, which is part of the current Ubuntu repositories, here.

In 6 Fairly Easy Steps

1. Add the breezy repositories containing nail to your source file
Edit your sources list

$ sudo gedit /etc/apt/sources.list

Add the following lines at bottom. These will enable you to apt-get nail

# breezy repositories (added to install nail)
# see http://old-releases.ubuntu.com/releases/ for more info
deb http://old-releases.ubuntu.com/ubuntu/ breezy universe

Don’t forget to update

$ sudo apt-get update

2. Install the needed programs

$ sudo apt-get install msmtp
$ sudo apt-get install nail

3. Install Thawte certificates for Gmail

EDIT: verisign.com apparently no longer issues certs at the address below. So the ‘wget’ step will not work. According to this comment, you can downloaded the needed cert from http://www.cs.utexas.edu/~suriya/UT-wireless/ThawtePremiumServerCA_b64.txt. I cannot vouch for this as my cert still works. But you might try downloading that and putting it your ~/etc/.certs directory if you’re having issues finding the cert.

This is necessary for Gmail. (Thanks to laurentbois.com.)

$ 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

4. Configure msmtp
This will open up a new msmtp configuration file

$ gedit ~/.msmtprc

Copy the following lines. Replace UPPERCASE text with your personal settings:

# 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 YOURPASSWORD

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

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

Change permission on this file or msmtp will complain:

$ chmod 600 ~/.msmtprc

5. Configure nail

$ gedit ~/.mailrc

Copy the following lines and replace UPPERCASE text with your personal settings:

# set smtp for nail
# ref: http://ubuntuforums.org/showpost.php?p=4531994&postcount=6

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

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

6. Run a test
Send a test message to your gmail account:

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

Check your gmail account and you should have a message from yourself. You can also check your log:

$ gedit /tmp/msmtp.log

That’s it. You should now be ready to incorporate email into your bash scripts, which is great for making backups. I did successfully test this on an old laptop had installed Ubuntu 8.04 this weekend.

If you have any problems or questions, I’d recommend posting to the ubuntu forums thread as you’ll probably get a quicker response there.

Advertisements
Command Line Email on Ubuntu (nail version)

5 thoughts on “Command Line Email on Ubuntu (nail version)

  1. Mitch says:

    In previous comment single quotes are needed – not slashes:

    cp ‘Thawte Server Roots/ThawtePremiumServerCA_b64.txt’ ThawtePremiumServerCA.crt

    Like

  2. Alessandro says:

    I’ve unpacked the zip file but I can’t find the “ThawtePremiumServerCA_b64.txt”.
    Tried to go on with another file called “Thawte Premium Server CA.txt” but in the final test I get this error:
    send-mail: cannot set X509 trust file /home/USER/etc/.certs/ThawtePremiumServerCA.crt for TLS session: Error while reading file.
    send-mail: could not send mail (account default from /home/alex/.msmtprc)

    Can you help me?

    Like

  3. Eduardo says:

    Hello

    This seems to be the answer for my problem. I have dotproject insatlled on my linux box and I need to configure the email notification (using a gmail account).

    How can I set msmtp as above for the whole system (not just for me)?

    Many thanks

    Ed

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s