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


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. 

[email protected]
[email protected]

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

# Where will the mail seem to come from?

# The full hostname

# 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




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]

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

(Ctrl+D to end the edit content)

test@instance-1test:~$ ls  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:~$ cat email.txt
Subject: Sending email using ssmtp
Testing email body

Also you can use -vvv option to get more outputs:

root@ip-172-31-2-10:/etc/ssmtp# ssmtp -vvv [email protected]
[<-] 220 ESMTP f18sm401723lfc.233 - gsmtp
[->] EHLO
[<-] 250 SMTPUTF8
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO
[<-] 250 SMTPUTF8
[<-] 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 (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
[->] .
[<-] 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 e23sm403112lfc.222 - gsmtp
ssmtp: 530 5.7.0 e23sm403112lfc.222 - gsmtp

[<-] 535 5.7.8 u5sm595090ljd.11 - gsmtp
ssmtp: Authorization failed (535 5.7.8 u5sm595090ljd.11 - gsmtp)

[<-] 534 5.7.14 x14sm397838lfg.165 - gsmtp
ssmtp: Authorization failed (534 5.7.14 x14sm397838lfg.165 - gsmtp)

1. If you are getting authentication error:

2. If you are getting authorization error
  • Go to 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 and click Continue to allow new device to sign it. 
  • Try signing in again from the linux command line. 


No comments:

Post a Comment