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

Latest

Learning, Sharing, Creating

Cybersecurity Memo

Wednesday, February 10, 2021

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

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 will then wait for you to type your message, 





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

(Ctrl+D to end the edit content)

test@instance-1test:~$ ls
Mailx.sh  dead.letter  email.txt
test@instance-1test:~$ more email.txt
From: [email protected]
Subject: test email
Hello World!


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



Also you can use -vvv option 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

Google Account Security Requirement

Some Error Message you might received during Google Account Security requirements:


[<-] 530 5.7.0  https://support.google.com/mail/?p=WantAuthError e23sm403112lfc.222 - gsmtp
ssmtp: 530 5.7.0  https://support.google.com/mail/?p=WantAuthError e23sm403112lfc.222 - gsmtp




[<-] 535 5.7.8  https://support.google.com/mail/?p=BadCredentials u5sm595090ljd.11 - gsmtp
ssmtp: Authorization failed (535 5.7.8  https://support.google.com/mail/?p=BadCredentials u5sm595090ljd.11 - gsmtp)




[<-] 534 5.7.14  https://support.google.com/mail/answer/78754 x14sm397838lfg.165 - gsmtp
ssmtp: Authorization failed (534 5.7.14  https://support.google.com/mail/answer/78754 x14sm397838lfg.165 - gsmtp)

1. If you are getting authentication error:



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://accounts.google.com/DisplayUnlockCaptcha and click Continue to allow new device to sign it. 
  • Try signing in again from the linux command line. 

References:




No comments:

Post a Comment