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
Install the ssmtp package:
Configure the SSMTP config file.
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]
[email protected]#
#
# 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
[email protected]
UseSTARTTLS=YES
UseTLS=Yes
Optional configuration
Adding reverse aliases
Test sSMTP
sSMTP will then wait for you to type your message,
[email protected]:~$ ssmtp [email protected]
hello test
test
^D
(Ctrl+D to end the edit content)
[email protected]:~$ ls
Mailx.sh dead.letter email.txt
[email protected]:~$ more email.txt
From: [email protected]
Subject: test email
Hello World!
[email protected]:~$ ssmtp [email protected] < email.txt
[email protected]:~$
[email protected]:~$ cat email.txt
Subject: Sending email using ssmtp
Testing email body
[email protected]:~$
Also you can use -vvv option to get more outputs:
[email protected]:/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
[<-] 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)
- 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://accounts.google.com/DisplayUnlockCaptcha and click Continue to allow new device to sign it.
- Try signing in again from the linux command line.
References: