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.

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:

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
60 comments… add one
  • 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

  • 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

  • crash Sep 30, 2015 @ 0:36

    worked wonderfully!

    thaanks!

  • 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

  • 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.”

  • 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?

  • 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

  • Rajdeep Mukherjee Jun 8, 2013 @ 8:45

    still works like a charm 🙂

  • John Jun 1, 2013 @ 21:16

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

  • Unsatified Nov 6, 2012 @ 4:34

    You tutorial doesn’t work

  • 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”)

  • robert Apr 20, 2012 @ 11:07

    Hi,

    Thank you very much!
    Exactly what I needed.

    :))

  • Danny Mar 15, 2012 @ 8:48

    Kudos for a simple-to-use solution!

  • 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.

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.