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

by Vivek Gite · 29 comments

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.

Further readings:

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 29 comments… read them below or add one }

1 Alexei 04.05.09 at 8:06 pm

Typo in the header: relay.

2 Vivek Gite 04.06.09 at 3:13 am

Thanks for the heads-up.

3 Nabin Limbu 04.06.09 at 10:41 am

Thanks for the info. Today I was searching for a way out for logwatch to send email via using external SMTP without installing sendmail or other MTA in my cache server and then after an hour I got this solution. Thanks

4 sanzle 04.06.09 at 1:44 pm

hi, thanks for the wonderful tip..
i went through the conf file…….but i m stuck somewhere in between…

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=sanzle-desktop

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES

should i update this file adding the lines you hav provided..and what abt the disabling Sendmail…I can find those mentioned lines..

Thanks in advance.

5 Ulver 04.06.09 at 2:02 pm

another good program like sstmp are email (encrypted email not electronic email !! ) you could see at http://www.cleancode.org/projects/email it have similar features than ssmtp plus the file attachment by command line.

Cheers and happy shell mailing ! xD

6 Vivek Gite 04.06.09 at 2:17 pm

Yes, you should update configuration by adding appropriate values.

Command related to sendmail disabling works on RHEL / Fedora and CentOS.

HTH

7 Vivek Gite 04.06.09 at 2:23 pm

@Ulver,

Thanks for sharing email project.

8 neeraj 04.06.09 at 5:51 pm

Hello !
thanks for sharing your knowledge.
I am using Ubuntu 8.04.
when i opened .conf file i got the following script:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=nee-pc

# Are users allowed to set their own From: address?
# YES – Allow the user to specify their own From: address
# NO – Use the system generated From: address
#FromLineOverride=YES
~
~
Now how to update this file with tha changes you provided??
pls tell me!
thanka in advance!

9 Ulver 04.06.09 at 6:21 pm

thanks, no worries

10 Vivek Gite 04.06.09 at 6:42 pm

Yes, you have to add changes provided by me. If there is already config line update it; otherwise just append new config line.

11 Balaviswanathan.V 04.07.09 at 2:26 am

Thanks a lot Vivek for the tip that you have given us and please continue the good work
:)

12 Rabindra Karki 04.07.09 at 6:13 am

Thanks for the tips

13 kgas 04.07.09 at 2:08 pm

I am using this already. I read this earlier here http://tombuntu.com/index.php/2008/10/21/sending-email-from-your-system-with-ssmtp/. I have also seen some articles in tombuntu referring to Vivek. Thanks to read this once again to refresh :D.

14 Chris 04.07.09 at 2:59 pm

This is perfect us on consumer Internet lines where SMTP ports are blocked by ISP. Brilliant! Thanks, Vivek.

15 Thomas Moersch 04.08.09 at 5:49 pm

Love this blog! I look after around 100 linux servers. There have been many useful tips and contributions is this blog.
Really useful!
;-)

Thanks!

16 Mehran Khajavi 04.28.09 at 12:59 pm

easy to use and interesting

17 Han Solo 04.28.09 at 8:18 pm

I use MSMTP for bellsouth where they accept mail from CLIENTS but not from sendmail.
http://msmtp.sourceforge.net/

You just replace sendmail with this and it looks like a mail client to your ISP.

18 charu 05.06.09 at 9:34 am

Good man…

I love this blog……

19 JackBeeNimble 05.06.09 at 7:57 pm

what about creating a .forward file in your /home/myname instead of all this. And you can add as many emails as you like… homename@gmail.com,myworkname@work.com

20 Aleksey Zalesov 05.22.09 at 11:50 am

I have a problem sending mail to the same domain.

For example, I have foo.ru configured in Google Apps. From 1@foo.ru I can send email to 2@bar.ru, but not to 2@foo.ru. Can you help me resolve this problem?

21 Aleksey Zalesov 05.22.09 at 11:55 am

Sorry, just big delay in mail delivery. Everything works fine!

22 pranay 06.01.09 at 9:34 am

HI , i made the above configurations as stated but when i a m sending mail to test then the following error is being showed:
/usr/sbin/sendmail: No such file or directory
“/root/dead.letter” 9/232
. . . message not sent.
please help

23 Sergio Montini 06.08.09 at 9:51 am

Great! I can now email directly from my NSLU2 (Slug).

Ideally, I would like a script to email me my system logs (cron, login, etc)

Any suggestions?

Many thanks!

24 Andres Rodriguez Guapacha 06.23.09 at 3:53 pm

Thanks for the Info! I have a Debian Squeeze system and when I tried to install ssmtp I couldn’t because exim4 was already installed. aptitude gave me the option to uninstall it, but I decided to try and use exim4 with gmail, so I ended in http://wiki.debian.org/GmailAndExim4 and followed those steps. It now works perfectly!

25 Robert McKenzie 07.12.09 at 2:11 am

Awesome .. exactly what I needed for the machines at home .. I refuse to use my ISP’s unauthenticated mail server, mostly because it’s a bit of a blackhole.. and I’ve been moving away from using mail on my hosted servers … most stuff has been ported to gmail servers.

Thanks for the quick and easy fix to using gmail as my smarthost!

26 Asaduzzaman 07.16.09 at 7:25 am

Dear Sir/ Madam,
Recently I have configured sandmail on Linux RH 9, and capable to send and receive mail from outlook express. But problem is domain name show diffenence in outlook express Inbox , that is : but I have configured domain name “bgdnyk.com” on linux send mail server.
Could you please asists, how can I get accurate domain name in outlook express.

27 anonymous 08.19.09 at 5:49 pm

OMG you are the best if it weren’t for you i’d still be messing around with nc/sendmail (sendmail.mc,m4)/telnet
I have a single user comp (root+my user) and this is perfect

28 Tim 08.25.09 at 4:14 pm

TO: Sergio Montini
[Ideally, I would like a script to email me my system logs (cron, login, etc)]

Here is a line of code that may help. It was pulled from Linux Pro Magazine August2009 issue, page 61. You should be able to add it to your backup script that cron launches. You’ll probably need to tweak it to suit your needs and not copy it verbatim, but this is the basic idea. Rather than sending the list of files in a backup directory, you could also send the contents of a log file.

ls -l /my-backups/’date +%y-%m-%d’ | mail -s “daily backup report” your@email.address

29 vuser1 12.16.09 at 4:02 am

Is anyone know how to config ssmtp to rewrite the RCPT TO header? Thanks.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: