Friday, May 13, 2011

How To Use Gmail Account To Relay Email From a Shell Prompt

SkyHi @ Friday, May 13, 2011
Usually, you do not need to setup an email server under Linux desktop operating system. Most GUI email clients (such as Thunderbird) supports Gmail POP3 and IMAP configurations. But, how do you send mail via the standard or /usr/bin/mail user agents or a shell script? Programs such as sendmail / postfix / exim can be configured as a gmail smarthost but they are largely overkill for this use.

You can use gmail as a smart host to send all messages from your Linux / UNIX desktop systems. You need to use a simple program called ssmtp. It accepts a mail stream on standard input with recipients specified on the command line and synchronously forwards the message to the mail transfer agent of a mailhub for the mailhub MTA to process. Failed messages are placed in dead.letter in the sender's home directory.

Install ssmtp

Type the following command under CentOS / RHEL / Red Hat / Fedora Linux:
# yum install ssmtp
Type the following command under Debian / Ubuntu Linux:
# apt-get update && apt-get install ssmtp

Configure gmail as a smarthost

Open /etc/ssmtp/ssmtp.conf, enter:
# vi /etc/ssmtp/ssmtp.conf
Update file with the following settings:
 
AuthUser=vivek@gmail.com
AuthPass=Your-Gmail-Password
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
 
Also, make sure you disable Sendmail:
# service sendmail stop
# chkconfig sendmail off
# mkdir /root/.bakup
# mv /usr/sbin/sendmail /root/.bakup
# ln -s /usr/local/ssmtp/sbin/ssmtp /usr/sbin/sendmail

Now, you can use mail / mailx command to send email messages. You can also write a shell script to backup your files and email to somewhere else (see below). You can test settings using following syntax:
$ echo "This is a test" | mail -s "Test" vivek@nixcraft.co.in

A note about sSMTP

sSMTP works well for desktop systems, but it is not a replacement for Sendmail / Postfix / Exim / Qmail for email server environment. This software is perfect for a single user system.

For the sake of others wondering.
# vim /etc/ssmtp/ssmtp.conf
# service sendmail stop
# chkconfig sendmail off
# mkdir /root/.backup
# mv /usr/sbin/sendmail /root/.backup/
# whereis ssmtp
ssmtp: /usr/sbin/ssmtp /etc/ssmtp /usr/share/man/man8/ssmtp.8.gz
# ln -s /usr/sbin/ssmtp /usr/sbin/sendmail
# echo “This is a test email” | mail -s “Test” adriyas@gmail.com

 

Further readings:


REFERENCES
http://www.cyberciti.biz/tips/linux-use-gmail-as-a-smarthost.html