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 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:
$ 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.
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
LikeLike
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.
LikeLike
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
LikeLike
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……..
LikeLike
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.
LikeLike
[…] some looking around, particularly this post on Ubuntu email with Mailx (which is incomplete, if you follow all the instructions, you will realise that the certificate he […]
LikeLike
Thank you!!
Two hours lost yesterday trying to get this happening, then I found this.
The fetching of the cert file is out of date, however, I had to scrounge and eventually found it here: http://www.cs.utexas.edu/~suriya/UT-wireless/ThawtePremiumServerCA_b64.txt
LikeLike
How to make it work with the new certificates. How to use the perm file.
LikeLike
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
LikeLike