Info Security Memo
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org

Build Confidence

Focusing on Information Security 

Info Security Notes

Using SSMTP to Send Email From Linux Command Line Through Gmail Account

2/10/2021

0 Comments

 
Using SSMTP to Send Email From Linux Command Line Through Gmail Account
Ssmtp (Simple SMTP) is a little nice tool you can use under Linux command line to send out email. SSMTP allows users to transfer emails through an SMTP server from the Linux command line. It provides the means to connect to a mailhub with a proper configuration file. If your config file was set up right, all your worries regarding command line email sending can disappear.



Updated Feb 2 2021

System Repository Update

Before we do anything else we should bring the Ubuntu Repository up to date. Type the following command to update Ubuntu System Repository:
sudo apt-get update

Install the ssmtp package:


sudo apt-get install ssmtp

Configure the SSMTP config file.


sudo vi /etc/ssmtp/ssmtp.conf

You can just remove all contents in the original file and just add the following in. 

UseSTARTTLS=YES
FromLineOverride=YES
[email protected]
mailhub=smtp.gmail.com:587
[email protected]
AuthPass=P@ssword1234#

Here is the new configuration file (Feb 10 2021), not much difference, but did add tls, root, hostname configuration in based on Internet research:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
[email protected]

# 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=smtp.gmail.com:587

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

# The full hostname
hostname=ip-172-31-2-10.eu-north-1.compute.amazonaws.com

# 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

#TLS_CA_FILE=/etc/pki/tls/certs/ca-bundle.crt

AuthUser=test1
AuthPass=P@ssword1

UseSTARTTLS=YES
UseTLS=Yes




Optional configuration
Adding reverse aliases
A reverse alias changes the "From" address. This means you can make the email appear as if it's from a different email address. I personally haven't done this but if it's something you'd like to do then edit the revaliases file as follows:
sudo vim /etc/ssmtp/revaliases
Then add a new line similar to this:
root:[email protected]:smtp.gmail.com:587



Test sSMTP

Once you've configurd sSMTP it's time to try and send an email. The simplest way to do this is to run sSMTP in a terminal with a recipient email address. So:
ssmtp [email protected]

sSMTP will then wait for you to type your message, 


jon_netsec@instance-1test:~$ ssmtp [email protected]
hello test
test
^D

(Ctrl+D to end the edit content)

jon_netsec@instance-1test:~$ ls
Mailx.sh  dead.letter  email.txt

jon_netsec@instance-1test:~$ more email.txt

From: [email protected]
Subject: test email
Hello World!

jon_netsec@instance-1test:~$ ssmtp [email protected] < email.txt
jon_netsec@instance-1test:~$
jon_netsec@instance-1test:~$ cat email.txt
Subject: Sending email using ssmtp
Testing email body
jon_netsec@instance-1test:~$





Also you can use -vvv to get more outputs:


root@ip-172-31-2-10:/etc/ssmtp# ssmtp -vvv [email protected]
[<-] 220 smtp.gmail.com ESMTP f18sm401723lfc.233 - gsmtp
[->] EHLO ip-172-31-2-10.eu-north-1.compute.amazonaws.com
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO ip-172-31-23-170.eu-north-1.compute.amazonaws.com
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6
[->] am9ubmV0c2Vj
[<-] 334 UGFzc3dvcmQ6
[<-] 235 2.7.0 Accepted
[->] MAIL FROM:<[email protected]>
[<-] 250 2.1.0 OK f18sm401723lfc.233 - gsmtp
[->] RCPT TO:<[email protected]>
[<-] 250 2.1.5 OK f18sm401723lfc.233 - gsmtp
[->] DATA
[<-] 354  Go ahead f18sm401723lfc.233 - gsmtp
[->] Received: by ip-172-31-23-170.eu-north-1.compute.amazonaws.com (sSMTP sendmail emulation); Wed, 10 Feb 2021 16:59:12 +0000
[->] From: "root" <[email protected]>
[->] Date: Wed, 10 Feb 2021 16:59:12 +0000
[->]
test1
[->] test1
[->] .
[<-] 250 2.0.0 OK  1612976360 f18sm401723lfc.233 - gsmtp
[->] QUIT
[<-] 221 2.0.0 closing connection f18sm401723lfc.233 - gsmtp


Notes: Google Account Requirement


1. If you are getting authentication error:
  • Open your Google Account.
  • In the "Security" section, select 2-Step Verification. You might need to sign in.
  • Select Turn off.
  • A pop-up window will appear to confirm that you want to turn off 2-Step Verification. Select Turn off.
  • Allow less secure apps: If you don't use 2-Step Verification, you might need to allow less secure apps to access your account.  (Go to https://www.google.com/settings/security/lesssecureapps and Turn On this feature)



2. If you are getting authorization error
  • Go to https://g.co/allowaccess from a different device you have previously used to access your Google account and follow the instructions. Click Continue button to allow new device to sign in. 

  • Go to https://ift.tt/1hVw7Zd and click Continue to allow new device to sign it. 
  • Try signing in again from the linux command line. 

References:

  • how to fix “send-mail: Authorization failed 534 5.7.14 ”




via Blogger https://ift.tt/2Z5jUig
February 10, 2021 at 12:27PM Linux
0 Comments



Leave a Reply.

    Categories

    All
    Architecture
    Blog
    Checkpoint
    Cisco
    Cloud
    CyberArk
    F5
    Fortigate
    Guardium
    Juniper
    Linux
    Network
    Others
    Palo Alto
    Qualys
    Raspberry Pi
    Security
    SIEM
    Software
    Vmware
    VPN
    Wireless

    Archives

    March 2024
    February 2024
    January 2024
    December 2023
    November 2023
    October 2023
    September 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022
    November 2022
    October 2022
    September 2022
    August 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    October 2021
    September 2021
    August 2021
    July 2021
    June 2021
    May 2021
    April 2021
    March 2021
    February 2021
    January 2021
    December 2020
    November 2020
    October 2020
    September 2020
    August 2020
    July 2020
    October 2019
    September 2019
    June 2019
    July 2018
    May 2018
    December 2017
    August 2017
    April 2017
    March 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    September 2015
    August 2015
    July 2015
    June 2015
    May 2015
    April 2015
    March 2015

    Print Page:

    RSS Feed

    Email Subscribe
Powered by Create your own unique website with customizable templates.
  • Blog
  • Sitemap
    • Categories
  • Contact
  • About
  • Resources
  • Tools
  • 51sec.org