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

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.

ADVERTISEMENTS

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:

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
60 comments… add one
  • Alexei Apr 5, 2009 @ 20:06

    Typo in the header: relay.

    • satya Sep 19, 2014 @ 22:51

      “/root/dead.letter” 9/224
      . . . message not sent.

      unable to send email getting error
      ^C

      • Ron Feb 17, 2015 @ 22:53

        I am getting the same message…

        [sudo] password for ron:
        [ron@localhost ~]$ /usr/sbin/sendmail: No such file or directory
        “/home/ron/dead.letter” 9/227
        . . . message not sent.

        [ron@localhost ~]$

  • 🐧 nixCraft Apr 6, 2009 @ 3:13

    Thanks for the heads-up.

  • Nabin Limbu Apr 6, 2009 @ 10:41

    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

  • sanzle Apr 6, 2009 @ 13:44

    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.

    • 🐧 nixCraft Apr 6, 2009 @ 14:17

      Yes, you should update configuration by adding appropriate values.

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

      HTH

  • Ulver Apr 6, 2009 @ 14:02

    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

  • 🐧 nixCraft Apr 6, 2009 @ 14:23

    @Ulver,

    Thanks for sharing email project.

  • neeraj Apr 6, 2009 @ 17:51

    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!

    • Svetlio Dec 4, 2016 @ 13:50

      You can eddit it with the command nano .

  • Ulver Apr 6, 2009 @ 18:21

    thanks, no worries

  • 🐧 nixCraft Apr 6, 2009 @ 18:42

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

  • Balaviswanathan.V Apr 7, 2009 @ 2:26

    Thanks a lot Vivek for the tip that you have given us and please continue the good work
    πŸ™‚

  • Rabindra Karki Apr 7, 2009 @ 6:13

    Thanks for the tips

  • kgas Apr 7, 2009 @ 14:08

    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.

  • Chris Apr 7, 2009 @ 14:59

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

  • Thomas Moersch Apr 8, 2009 @ 17:49

    Love this blog! I look after around 100 linux servers. There have been many useful tips and contributions is this blog.
    Really useful!
    πŸ˜‰

    Thanks!

  • Mehran Khajavi Apr 28, 2009 @ 12:59

    easy to use and interesting

  • Han Solo Apr 28, 2009 @ 20:18

    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.

  • charu May 6, 2009 @ 9:34

    Good man…

    I love this blog……

  • JackBeeNimble May 6, 2009 @ 19:57

    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

  • Aleksey Zalesov May 22, 2009 @ 11:50

    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?

  • Aleksey Zalesov May 22, 2009 @ 11:55

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

  • pranay Jun 1, 2009 @ 9:34

    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

  • Sergio Montini Jun 8, 2009 @ 9:51

    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!

  • Andres Rodriguez Guapacha Jun 23, 2009 @ 15:53

    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!

  • Robert McKenzie Jul 12, 2009 @ 2:11

    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!

  • Asaduzzaman Jul 16, 2009 @ 7:25

    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.

  • anonymous Aug 19, 2009 @ 17:49

    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

  • Tim Aug 25, 2009 @ 16:14

    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

  • vuser1 Dec 16, 2009 @ 4:02

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

  • Lukas Mar 23, 2010 @ 15:45

    I Got this error? Any help ? Pleaseeeeee

    $ echo “This is a test” | mail -s “Test” sakal@sakal.sk
    -su: $: command not found
    Null message body; hope that’s ok
    mail: /usr/sbin/sendmail: No such file or directory
    Can’t send mail: sendmail process failed with error code 1

    Thanks

  • Lukas Mar 23, 2010 @ 15:45

    also :

    ls -latr /usr/sbin/ | grep mail
    -rwxr-sr-x 1 root mail 81304 2008-12-30 21:34 imapd*
    lrwxrwxrwx 1 root root 5 2010-03-23 16:36 mailq -> ssmtp*
    lrwxrwxrwx 1 root root 27 2010-03-23 16:40 sendmail -> /usr/local/ssmtp/sbin/ssmtp

  • Me Mar 25, 2010 @ 20:04

    Any way to add multiple email accounts to this list? For example user1 and user2?

  • Ben May 14, 2010 @ 11:38

    So much easier than some of the other overkill guides – thanks!

  • Mats Karlsson Oct 10, 2010 @ 9:31

    There is some parts missing for CentOS 5:
    ssmtp is available in the epel repository, that needs to be added before “yum install ssmtp”

    The softlink is a bit off:
    ln -s /usr/sbin/ssmtp /usr/sbin/sendmail

    And I love this article, made my day πŸ˜‰

  • faerrleah Jan 17, 2011 @ 13:05

    Will get your gmail account blocked for abusing service if you send too much email and too often.

  • adriyas Apr 10, 2011 @ 4:43

    As always, Thanks Vivek.

    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

    Used it for my nagios email notification for repl

  • msk Jun 23, 2011 @ 10:31

    I have tried above mentioned configured on my system. It worked nicely for a day but now it shows me following error.
    “SSL_connect: Connection reset by peer. send-mail: Cannot open smtp.gmail.com:587”

    Please get me to resolve this issue.

  • Daniel Kanafany Oct 12, 2011 @ 17:54

    for those who has problems like this:

    Null message body; hope thatÒ€ℒs ok
    mail: /usr/sbin/sendmail: No such file or directory
    CanÒ€ℒt send mail: sendmail process failed with error code 1

    try this:

    the manual says that we must create a symlink:

    ln -s /usr/local/ssmtp/sbin/ssmtp /usr/sbin/sendmail

    but, depends on ur distro or version the correct location of this directories. In my case I use Fedora 12-64 and find that “/usr/local/ssmtp/sbin/ssmtp” is “/usr/sbin/ssmtp”, why:

    [root@kanafas ssmtp]# whereis ssmtp
    ssmtp: /usr/sbin/ssmtp /etc/ssmtp /usr/share/man/man8/ssmtp.8.gz

    so I change the link to ” ln -s /usr/sbin/ssmtp /usr/sbin/sendmail ”

    and now is working, any doubts or comments Ill be glad to help you.

  • Best Brain Teaser Nov 13, 2011 @ 16:54

    /usr/sbin/sendmail: No such file or directory
    “/home/lavesh/dead.letter” 9/227
    . . . message not sent.

  • Bill Cunningham Nov 23, 2011 @ 15:54

    Outstanding information! This worked exactly right the first time. I have been mucking around with mail on a Fedora PC for a long time. This did the trick.

  • William Notowidagdo Feb 2, 2012 @ 6:50

    Works like a charm. Thanks for sharing.

  • saravanan Feb 5, 2012 @ 15:32

    I have a problem with sending mail in CentOS 5.6.
    I need a script to send mail from gmail to other accounts.
    Please help me.

  • Danny Mar 15, 2012 @ 8:48

    Kudos for a simple-to-use solution!

  • robert Apr 20, 2012 @ 11:07

    Hi,

    Thank you very much!
    Exactly what I needed.

    :))

  • 419 scambaiter Jun 18, 2012 @ 7:02

    google doesn’t like it when you help others “weaponize gmail”.

    I have used gmail to torment 419 scammers… and subsequently had my accounts frozen

    beware the do-not-evil (which is NOT “do good”)

  • Unsatified Nov 6, 2012 @ 4:34

    You tutorial doesn’t work

  • John Jun 1, 2013 @ 21:16

    The first instructions posted 4 years ago, and still useful. Thanks.

  • Rajdeep Mukherjee Jun 8, 2013 @ 8:45

    still works like a charm πŸ™‚

  • beatengun Jul 1, 2013 @ 13:18

    [root@localhost /]# yum install ssmtp
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * addons: centos.excellmedia.net
    * base: centos.excellmedia.net
    * extras: centos.excellmedia.net
    * updates: centos.excellmedia.net
    Setting up Install Process
    No package ssmtp available.
    Nothing to do

    WHY THIS KOLAVERI DI?

    • beatengun Jul 1, 2013 @ 13:23

      My OS is CentOS

    • kusuma suresh Nov 7, 2014 @ 11:08

      You need to install epel repository before going to install ssmtp

  • Yogesh Jul 24, 2014 @ 8:01

    Is ssmtp not available with “epel” repo of RHEL7? is not, what could be used as an alternative of ssmtp in RHEL7, for sending out emails using gmail?

  • kusuma suresh Nov 7, 2014 @ 11:08

    Hi,
    Unable to send mail’s getting error as below
    “/usr/sbin/sendmail: No such file or directory
    “/root/dead.letter” 9/220
    . . . message not sent.”

  • Bruno Vernay Dec 16, 2014 @ 14:06

    You can create a dedicated Google password just for Gmail, that you are able to revoke at any time: https://support.google.com/accounts/answer/185833?hl=en

  • crash Sep 30, 2015 @ 0:36

    worked wonderfully!

    thaanks!

  • ddd12 May 29, 2016 @ 20:25

    One of the main reasons this method won’t work is that the ssmtp command may be located on a different directory. This is the updated version of the command lines in order to disable send mail:
    # service sendmail stop
    # chkconfig sendmail off
    # mkdir /root/.bakup
    # mv /usr/sbin/sendmail /root/.bakup
    # ln -s $(which ssmtp) /usr/sbin/sendmail

  • riya Jul 21, 2017 @ 10:59

    echo “This is a test” | mail -s “Test” nimisha.mec@gmail.com
    mail: cannot send message: Process exited with a non-zero status

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.