Command Line Email on Ubuntu (mailx version)

This guide explains how to configure Ubuntu so that you can send email from the command line using mailx with your Gmail account for delivery. This updates a guide I originally posted on the ubuntu forums for mailx, which is part of the current Ubuntu repositories (versions 7 and 8). To use nail, for which this guide was originally drawn up, see this post.

In 5 Fairly Easy Steps

1. Install the needed programs

$ sudo apt-get install msmtp
$ sudo apt-get install mailx

2. Install Thawte certificates for Gmail
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

3. 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

# 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 (not necessary with single account)
#account default : gmail

Change permission on this file or msmtp will complain:

$ chmod 600 ~/.msmtprc

4. Configure mailx

$ 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)
# $ mailx -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"

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

$ echo -e “testing email from the command line” > /tmp/test_email
$ mailx -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

I personally prefer nail because it more easily accommodates multiple accounts. But if you don’t have a deliberate need to use multiple accounts and are using a later version of Ubuntu, use this.

3 Comments

  1. Mitch says:

    Thanks a lot for this. I think that a couple of slashes are missing in a line. Should be:

    $ cp Thawte\ Server\ Roots/ThawtePremiumServerCA_b64.txt ThawtePremiumServerCA.crt

  2. Stephen says:

    Thank you so much for this info. Unlike other HOW-TOs, this one worked! Now I can send mail successfully from the command line. My next step is to get my scripts to use it as successfully.

  3. Pyro-X says:

    Thank you very much for your howto. It worked fine except for the certificate not named the same way at the current certificates file (https://www.verisign.com/support/thawte-roots.zip)

    I made it work using this perm file from the zip:
    Thawte SSLWeb Server Roots/thawte Premium Server CA/Thawte Premium Server CA.pem

    :)

    Thanks again

Leave a Reply