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 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.)
$ 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
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:
4. Configure mailx
Copy the following lines and replace UPPERCASE text with your personal settings:
# set smtp for nail # ref: http://ubuntuforums.org/showpost.php?p=4531994&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;postcount=6 # gmail account (default) # $ mailx -s "subject line" -a /path/file email@example.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:
$ 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:
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.