Command Line Email on Ubuntu (mailx version)

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

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

EDIT: verisign.com apparently no longer issues certs at the address below. So the ‘wget’ step will not work. adkein, in a comment below (click here), links to another page when you can apparently get the needed cert. 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

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.

Command Line Email on Ubuntu (mailx version)

9 thoughts on “Command Line Email on Ubuntu (mailx version)

  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

    Like

  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.

    Like

  3. hi…
    i just did follow the steps as suggested by u by ended up getting

    send-mail: account default not found in /home/rakshith/.msmtprc
    Can’t send mail: sendmail process failed with error code 78

    can u please help me out…
    thanks……..

    Like

  4. Andres says:

    Thanks for the guide! I also coudldn’t get the .crt file to work, but was able to get it working with the .pem file (like a previous commenter). I hope this is also secure!

    I also had to use single quotes in the subject line rather than double.

    Like

  5. Pradeep Kumar says:

    Hi All,

    Please help me out, I have sorted out almost all the errors, Finally, was not able to understand these below errors. Please help

    ====
    pradeep@pradeep:~/etc/.certs$ mailx -s “nail gmail test” kondapalli.pradeep@gmail.com < /tmp/test_email
    send-mail: recipient address gmail not accepted by the server
    send-mail: server message: 553-5.1.2 We weren't able to find the recipient domain. Please check for any
    send-mail: server message: 553-5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,
    send-mail: server message: 553 5.1.2 or other punctuation after the recipient's email address. 22sm709886qyk.14
    send-mail: could not send mail (account default from /home/pradeep/.msmtprc)
    Can't send mail: sendmail process failed with error code 65

    ====

    Thanks and Regards,
    Pradeep Kumar

    Like

Leave a comment