• Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Send Email via SMTP Server from Linux Command Line (with SSMTP)

Written by Rahul, Updated on December 10, 2018

In the Global word, we always want to keep connected to everyone. To accomplish this email is also a very popular method to send and receive information. Linux systems also provide tools to send emails from the command line which is used to get system details, to send emails from shell scripts etc.

When we simply send email from Linux terminal, email sends as system [email protected] Some of the SMTP servers can block these emails or mark them as spam. So we need a process which maximizes the email delivery to the inbox. Using this article we are configuring our server to send email from SMTP servers like Gmail, Amazon SES etc. This article will help you setup SSMTP server and send email through Gmail servers.

Step 1 – Install SSMTP Server

SSMTP service packages are available under EPEL repository, So make you have EPEL yum repository configured in your system.

yum install ssmtp

Step 2 – Configure SSMTP

Now edit SSMTP configuration file and add the following values. For this tutorial, I am using the Gmail SMTP server. If you also want to set up with Gmail, make sure you have an email account with Gmail.

vim /etc/ssmtp/ssmtp.conf

Change the following values in the configuration file

 mailhub=smtp.gmail.com:587
 UseSTARTTLS=YES
 [email protected]
 AuthPass=XXXXXXXXXXXXXXX
 TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt

The above details are as followings,

  • mailhub: your smtp server host/ip with port.
  • UseSTARTTLS: Set it Yes if SMTP server uses TLS else No.
  • AuthUser: Use Gmail ID here
  • AuthPass: Use Gmail ID’s password
  • TLS_CA_File: This may required some time, If you face issue like “send-mail: Cannot open smtp.gmail.com:587”

Step 3 – Send Test Email

Now test the setup by sending a test email to an email address. First compose the mail in text file like:

vim msg.txt

Add email content, for example:

msg.txt
Subject: This is Subject Line

Email content line 1
Email content line 2

Then send email to “[email protected]” using the ssmtp command:

ssmtp [email protected] < msg.txt

Let's check your mailbox for the new email.

Step 4 - Setup SSMTP as Default

Now set SSMTP as your default mail server, So that you can simply use mail command to send emails through SSMTP.

alternatives --config mta

There are 2 programs which provide 'mta'.

  Selection    Command
-----------------------------------------------
   1           /usr/sbin/sendmail.ssmtp
*+ 2           /usr/sbin/sendmail.sendmail

Enter to keep the current selection[+], or type selection number: 1

Verify the changes by

sendmail -V
sSMTP 2.61 (Not sendmail at all)
  • Read - 5 Ways to Send Email From Linux Command Line

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

15 Comments

  1. Avatar Bishwanath Reply
    January 7, 2021 at 4:52 pm

    Smtp configure in Linux cell manager

  2. Avatar Proyash Reply
    September 22, 2019 at 2:55 pm

    ssmtp: 530 5.7.0 Must issue a STARTTLS command first. a6sm7530382pgb.34 – gsmtp
    why does this error kept coming?

  3. Avatar Nikhila Reply
    August 11, 2018 at 6:55 am

    Hello,
    I tried mailing using SSMTP and mailutils even.But both resulted me an error saying “Cannot open smtp.gmail.com:587″ and ” Process exited with a non-zero status”.

    I have been trying for past 3 days to get this right but failed everytime. I tried by adding TLS_CA_FILE… command in config file too but it has also gone in vain.

    Please provide me a solution

    Thnaks in advance

  4. Avatar haythem Reply
    March 1, 2018 at 8:48 am

    hello,
    i am working on writing a script to send the email.
    is there any solution to use, instead of “^d” to make it work in a script.
    thanks

    • Avatar Rudolf Frees Reply
      December 9, 2018 at 1:26 pm

      Hello,

      just configured ssmtp following Rahul’s helpful instructions – immidiate success!
      Now I have the same question as you:
      Is there any solution to use, instead of “^d” to make it work in a script?

      If you found such a solution it would be nice to read about it.

      Thank you in advance

      • Rahul Rahul Reply
        December 10, 2018 at 7:19 am

        Hi HAYTHEM & RUDOLF,

        I have updated the Step 3 in the tutorial. Hope this will help you to send emails via script.

  5. Avatar Rohit Kumar Reply
    October 23, 2017 at 3:35 pm

    Currently I’m using Nextehost.com SMTP but planning to build own server for Email marketing. hope this will be more cheaper.

    suggest me what I need ? I send 10k-15K email on alternate days.

    • Rahul Rahul K. Reply
      October 24, 2017 at 4:42 am

      Hi Rohit, You can use Postal SMTP server.

      https://tecadmin.net/install-postal-mail-server-on-ubuntu/

  6. Avatar Skatox Reply
    October 24, 2016 at 1:12 am

    Nice tutorial!

  7. Avatar SUNEET Reply
    September 15, 2015 at 1:53 am

    plz provide me soln.

    • Avatar crilose Reply
      May 17, 2016 at 6:55 pm

      I solved this by allowing google to accept connections from untrusted/not safe apps in this module: https://www.google.com/settings/security/lesssecureapps
      Of course be careful when activating this!

  8. Avatar SUNEET Reply
    September 15, 2015 at 1:52 am

    ssmtp: Authorization failed (534 5.7.14 https://support.google.com/mail/answer/78754 as4sm18579248pbc.48 – gsmtp)

  9. Avatar Mike Reply
    February 3, 2015 at 1:56 am

    Thanks for the great write-up. Do you know what the syntax looks like to send to multiple recipients from the command line?

    I’ve tried adding Cc: in a separate text file and doing ssmtp [email protected] < message.txt
    but it looks like this just makes it appear like the other recipients were CC'd, without actually sending the message to them.

  10. Avatar Young man Reply
    November 10, 2014 at 3:51 pm

    Hello,
    When i run this command # vim i got this
    VIM – Vi IMproved
    ~
    ~ version 7.2.411
    ~ by Bram Moolenaar et al.
    ~ Modified by
    ~ Vim is open source and freely distributable
    ~
    ~ Help poor children in Uganda!
    ~ type :help iccf for information
    ~
    ~ type :q to exit
    ~ type :help or for on-line help
    ~ type :help version7 for version info

    which is different from your next Step what is wrong?

    Hope to help

    • Avatar Luke Reply
      June 1, 2018 at 10:45 pm

      You will probably have an easier time if you use nano as your editor instead of vim.

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Debian 10
  • How to Install Python 3.9 on CentOS/RHEL 8
  • Download Ubuntu 20.04 LTS – DVD ISO Images
  • Linux Run Commands As Another User
  • How to Check PHP Version (Apache/Nginx/CLI)
  • How To Install and Configure GitLab on Ubuntu 20.04
  • How to Check Ubuntu Version with Command or Script
  • How to Install PyCharm on Ubuntu 20.04
© 2013-2021 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy