Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Mail Services»Sending Email via Remote SMTP in Linux (SSMTP)

    Sending Email via Remote SMTP in Linux (SSMTP)

    By RahulJanuary 8, 20234 Mins Read

    SMTP (Simple Mail Transfer Protocol) is a protocol for sending email messages between servers. Most email systems that send mail over the Internet use SMTP to send messages from one server to another, and to deliver messages to local mail clients like Microsoft Outlook or Apple Mail.

    Advertisement

    SSMTP is a simple and lightweight utility that allows users to send emails from the command line or shell scripts. It is especially useful for systems that don’t have a full-blown mail server installed, such as a headless server or a machine running on a cloud platform.

    In this article, we will show you how to set up and configure SSMTP on your Linux machine.

    Step 1: Install SSMTP

    The first thing you need to do is install SSMTP on your Linux machine. Depending on your distribution, you can use the package manager to install it.

    • For example, On a Debian or Ubuntu machine, you can use the following command:
      sudo apt update && apt install ssmtp 
      
    • On a CentOS or Fedora machine, you can use the following command:
      sudo yum install ssmtp 
      

    Step 2: Configure SSMTP

    Once SSMTP is installed, you need to configure it by editing the configuration file located at “/etc/ssmtp/ssmtp.conf”. For this tutorial, I am using the Gmail SMTP server. You can use any of the remote SMTP servers that allowed you to send emails from your system.

    Open the file in a text editor, such as nano or vi:

    sudo nano /etc/ssmtp/ssmtp.conf 
    

    Change/Update the following lines 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 to Yes if the SMTP server uses TLS or else No.
    • AuthUser: Use Gmail ID here
    • AuthPass: Use Gmail ID’s password
    • TLS_CA_File: This may be required sometimes If you face an issue like “send-mail: Cannot open smtp.gmail.com:587”

    Step 3: Send a Test Email

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

    nano msg.txt 
    

    Add email content, for example:

    msg.txt
    From: [email protected] Subject: This is the Subject Line Email content line 1 Email content line 2

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

    ssmtp [email protected] < msg.txt 
    

    Check your mailbox for the new email.

    Step 4: Set the Default Mail Transfer Agent

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

    alternatives --config mta 
    
    There are 2 programs that 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
    

    In case of no other mail client is configured, You should see a message like "no alternatives for mta". In that case, SSMTP is the default mail client.

    To verify the changes, execute:

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

    Conclusion

    In this article, we showed you how to set up and configure SSMTP on your Linux machine in 5 simple steps. First, we installed SSMTP using the package manager. Then, we configured SSMTP by editing the configuration file and setting the default mail transfer agent. After that, we tested SSMTP by sending an email from the command line. Finally, we showed you how to use SSMTP in shell scripts to send automated emails.

    SSMTP is a simple and lightweight utility that allows you to send emails from the command line or shell scripts. It is especially useful for systems that don't have a full-blown mail server installed, such as a headless server or a machine running on a cloud platform. With a few simple configurations, you can easily use SSMTP to send emails from your Linux machine.

    command-line Sen sendmail smtp ssmtp
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Configuring Sendmail through the External SMTP relay

    Postfix: Sending Emails From External SMTP Servers

    Postfix: Sending Emails From External SMTP Servers

    Postfix: Configure SASL Authentication for Remote SMTP

    Postfix: Configure SASL Authentication for Remote SMTP

    View 16 Comments

    16 Comments

    1. John Smith on November 5, 2021 10:13 am

      Very helpful, but I am having trouble with Step 4, where does the “alternatives” command come from, it’s not installed on my system.

      Reply
    2. Bishwanath on January 7, 2021 4:52 pm

      Smtp configure in Linux cell manager

      Reply
    3. Proyash on September 22, 2019 2:55 pm

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

      Reply
    4. Nikhila on August 11, 2018 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

      Reply
    5. haythem on March 1, 2018 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

      Reply
      • Rudolf Frees on December 9, 2018 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

        Reply
        • Rahul on December 10, 2018 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.

          Reply
    6. Rohit Kumar on October 23, 2017 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.

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

        Hi Rohit, You can use Postal SMTP server.

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

        Reply
    7. Skatox on October 24, 2016 1:12 am

      Nice tutorial!

      Reply
    8. SUNEET on September 15, 2015 1:53 am

      plz provide me soln.

      Reply
      • crilose on May 17, 2016 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!

        Reply
    9. SUNEET on September 15, 2015 1:52 am

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

      Reply
    10. Mike on February 3, 2015 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.

      Reply
    11. Young man on November 10, 2014 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

      Reply
      • Luke on June 1, 2018 10:45 pm

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

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.