klenwell information services : UbuntuCommandLineGmailNail

Revision [1371]

This is an old revision of UbuntuCommandLineGmailNail made by KlenwellAdmin on 2010-05-12 10:26:35.
 

Command Line Gmail Using msmtp/nail

Instructions on how to configure Ubuntu to enable email to be sent from the command line using your Gmail account. This is especially useful for automating local backups and other scripts.

This guide covers using the nail program, which is my client of choice but is no longer in the main repositories. (Apparently, it is now known as heirloom mailx.) Find a guide for using mailx, which is part of the current Ubuntu repositories

Overview

1. Add Beezy Repositories for Nail to Apt Sources
2. Install Necessary Packages
3. Install Thawte Certificate for Gmail
4. Configure msmtp
5. Configure mailx
6. Test

Steps

1. Install Necessary Packages

Edit your source list:
$ sudo gedit /etc/apt/sources.list


Add the following lines to the bottom of your sources.list file. 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 Necessary Packages

$ sudo apt-get install msmtp nail


3. Install Thawte Certificate

This requires a Thawte certificate. The steps below assume that it is still available at the provided url. If not, you can create a file from the text included below.
$ mkdir -p ~/etc/.certs
$ chmod 0700 ~/etc/.certs
$ cd ~/etc/.certs
$ wget http://userweb.cs.utexas.edu/~suriya/UT-wireless/ThawtePremiumServerCA_b64.txt


The certificate, in all its glory:
-----BEGIN CERTIFICATE-----
MIIDJzCCApCgAwIBAgIBATANBgkqhkiG9w0BAQQFADCBzjELMAkGA1UEBhMCWkEx
FTATBgNVBAgTDFdlc3Rlcm4gQ2FwZTESMBAGA1UEBxMJQ2FwZSBUb3duMR0wGwYD
VQQKExRUaGF3dGUgQ29uc3VsdGluZyBjYzEoMCYGA1UECxMfQ2VydGlmaWNhdGlv
biBTZXJ2aWNlcyBEaXZpc2lvbjEhMB8GA1UEAxMYVGhhd3RlIFByZW1pdW0gU2Vy
dmVyIENBMSgwJgYJKoZIhvcNAQkBFhlwcmVtaXVtLXNlcnZlckB0aGF3dGUuY29t
MB4XDTk2MDgwMTAwMDAwMFoXDTIwMTIzMTIzNTk1OVowgc4xCzAJBgNVBAYTAlpB
MRUwEwYDVQQIEwxXZXN0ZXJuIENhcGUxEjAQBgNVBAcTCUNhcGUgVG93bjEdMBsG
A1UEChMUVGhhd3RlIENvbnN1bHRpbmcgY2MxKDAmBgNVBAsTH0NlcnRpZmljYXRp
b24gU2VydmljZXMgRGl2aXNpb24xITAfBgNVBAMTGFRoYXd0ZSBQcmVtaXVtIFNl
cnZlciBDQTEoMCYGCSqGSIb3DQEJARYZcHJlbWl1bS1zZXJ2ZXJAdGhhd3RlLmNv
bTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0jY2aovXwlue2oFBYo847kkE
VdbQ7xwblRZH7xhINTpS9CtqBo87L+pW46+GjZ4X9560ZXUCTe/LCaIhUdib0GfQ
ug2SBhRz1JPLlyoAnFxODLz6FVL88kRu2hFKbgifLy3j+ao6hnO2RlNYyIkFvYMR
uHM/qgeN9EJN50CdHDcCAwEAAaMTMBEwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG
9w0BAQQFAAOBgQAmSCwWwlj66BZ0DKqqX1Q/8tfJeGBeXm43YyJ3Nn6yF8Q0ufUI
hfzJATj/Tb7yFkJD57taRvvBxhEf8UqwKEbJw8RCfbz6q1lu1bdRiBHjpIUZa4JM
pAwSremkrj/xw0llmozFyD4lt5SZu5IycQfwhl7tUCemDaYj+bvLpgcUQg==
-----END CERTIFICATE-----

source: http://userweb.cs.utexas.edu/~suriya/UT-wireless/ThawtePremiumServerCA_b64.txt

4. Configure msmtp

Create a new msmtp config 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_b64.txt

# 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


5. Configure mailx

Open mailx config file:
$ gedit ~/.mailrc


Copy the following lines and replace UPPERCASE text with your personal settings:
# set smtp for mailx

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


6. 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:
$ tail /tmp/msmtp.log


References

http://www.klenwell.com/press/2009/03/ubuntu-email-with-nail/
http://ubuntuforums.org/showthread.php?t=780509
http://phosphorusandlime.blogspot.com/2008/05/ubuntu-command-line-email.html
http://heirloom.sourceforge.net/mailx.html


CategoryUbuntu