klenwell information services : UbuntuCommandLineGmail

Command Line Gmail Using msmtp/mailx

return to CategoryUbuntu

For the nail version of this script (which I prefer!), see UbuntuCommandLineGmailNail

Now Available: Install Script

Gist: https://gist.github.com/tatwell/968a762038eaf6504807749c70bfad64

Installation

1. Download the script from http://code.google.com/p/klenwell/downloads/list. (Next steps assume you download it to dir /tmp.

2. To completely automate installation, update your gmail settings at top of script:
$ gedit /tmp/ubuntu_gmail_mailx-v11.10.sh


3. Make script executable and run with install command:
$ chmod 755 /tmp/ubuntu_gmail_mailx-v11.10.sh
$ /tmp/ubuntu_gmail_mailx-v11.10.sh install


Usage

Send a file with an attachment
$ sudo apt-get install sharutils
$ EMAIL_TO=YOURNAME@gmail.com
$ echo "email test" > /tmp/emailbody
$ TMP_ARCHIVE=/tmp/some_archive
$ FNAME=$(basename $TMP_ARCHIVE)
$ ( cat /tmp/emailbody ; uuencode $TMP_ARCHIVE $FNAME ) | mailx -s "email test" $EMAIL_TO




Manual Guide


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.

Overview

1. Install Necessary Packages
Install Thawte Certificate for Gmail
2. Configure msmtp
3. Configure mailx
4. Test

Steps

1. Install Necessary Packages

$ sudo apt-get install msmtp heirloom-mailx


Install Certificate

According to archlinux.org, Google is in the process of becoming it's own certificate authority and has switched to a "Google Certificate Authority" certificate rooted in Equifax.

This means you can user the existing certificate at /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt. Verify that that certificate exists and nothing else needs to be done at this step.

If you don't have the certificate for some reason, you can download it in this package here: http://packages.ubuntu.com/en/source/lucid/ca-certificates

2. 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 /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.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


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


4. Test

Send a test message to your gmail account:
$ echo -e "testing email from the command line" > /tmp/test_email
$ mailx -s "mailx 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-mailx/
http://ubuntuforums.org/showthread.php?t=780509
http://phosphorusandlime.blogspot.com/2008/05/ubuntu-command-line-email.html
http://wiki.archlinux.org/index.php/Msmtp
http://stackoverflow.com/questions/96326/korn-shell-code-to-send-attachments-with-mailx-and-uuencode/96636#96636


CategoryUbuntu
There are 2 comments on this page.
[Display comments]