klenwell information services : UbuntuCommandLineGmailNail

Command Line Gmail Using msmtp/nail

return to CategoryUbuntu

Although I prefer nail, there is also a mailx version of this script available here: UbuntuCommandLineGmail

Now Available: Install Script

Source: setup_gmail_command_line.sh
Download: ubuntu_gmail_nail-v1.0.sh

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_nail-v1.0.sh


3. Make script executable and run with install command:
$ chmod 755 /tmp/ubuntu_gmail_nail-v1.0.sh
$ /tmp/ubuntu_gmail_nail-v1.0.sh install


Usage

Send a file with an attachment
$ echo -e "testing email from the command line" > /tmp/test_email
$ nail -s "nail gmail test" -a /tmp/test_email YOURNAME@gmail.com < /tmp/test_email
$ tail /tmp/msmtp.log



Manual Installation


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, here: Command Line Gmail Using msmtp/mailx

Overview

1. Add Breezy Repositories for Nail to Apt Sources
2. Install Necessary Packages
3. Configure msmtp
4. Configure nail
5. 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. 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

# isp account
account isp
auth login
host SMTP.YOURISP.COM
port 25
user YOURNAME@ISP.COM
from YOURNAME@ISP.COM
password YOURPASSWORD

# 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 (from above)
account default : gmail


Change permission on this file or msmtp will complain:
$ chmod 600 ~/.msmtprc


4. Configure nail

Open nail config file:
$ 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&amp;amp;amp;amp;amp;postcount=6

# gmail account (default)
# $ nail -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"

# isp account (add -A option to command line)
# $ nail -A isp -s "subject line" -a /path/file recipient@email.com < /path/body.txt
account isp {
set from="YOURNAME@ISP.COM"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a isp"
}


5. Test

Send a test message to your gmail account:
$ echo -e "testing email from the command line" > /tmp/test_email
$ nail -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://linux.die.net/man/1/nail
http://heirloom.sourceforge.net/mailx.html
http://wiki.archlinux.org/index.php/Msmtp


CategoryUbuntu