SSMTP is a simple utility that can be used to send emails from Linux in a local system to the specified email address. It is just MTA; it does not receive mail, expand aliases, or manage a queue. It forwards automated emails to an external email address. Generally, it can be used to forward an automated email (like system alerts) from your system to an external email address.

This post will show you how to install and use SSMTP to send an email from the command-line interface.

Step 1 – Install SSMTP

The SSMTP package is included in the Ubuntu 20.04 default repository by default. You can install it by just running the following command:

apt update -y
apt-get install ssmtp -y

Once the SSMTP package is installed, you can proceed to the next step.

Step 2 – Configure SSMTP

Next, you will need to define your Gmail or other SMTP servers in SSMTP configuration file. You can define it in the /etc/ssmtp/ssmtp.conf file:

nano /etc/ssmtp/ssmtp.conf

Add the following lines:

FromLineOverride=YES
root=postmaster
mailhub=smtp.your-domain.com:587
hostname=ubuntu2004
[email protected]
AuthPass=your-password
FromLineOverride=YES
UseSTARTTLS=YES

Save and close the file when you are finished.

SSMTP is now configured to send an email to your SMTP server address.

Step 3 – Send an Email with SSMTP

First, create a text file and write some content:

nano file.txt

Add the following lines:

Subject: This is Subject Line

Email content line 1
Email content line 2

Save and close the file, then send an email with an attachment file.txt to the external address [email protected]:

ssmtp -v [email protected] < file.txt

If everything is fine, you should see the following output:

[<-] 220 and/or bulk e-mail. [->] EHLO ubuntu2004
[<-] 250 HELP [->] STARTTLS
[<-] 220 TLS go ahead [->] EHLO ubuntu2004
[<-] 250 HELP [->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6 [->] aGl0ZXNoamV0aHZhQGxpbnV4YnV6LmNvbQ==
[<-] 334 UGFzc3dvcmQ6
[<-] 235 Authentication succeeded [->] MAIL FROM:<root@ubuntu2004>
[<-] 250 OK [->] RCPT TO:<[email protected]>
[<-] 250 Accepted [->] DATA
[<-] 354 Enter message, ending with "." on a line by itself [->] Received: by ubuntu2004 (sSMTP sendmail emulation); Sun, 18 Apr 2021 08:05:14 +0000
[->] From: "root" <root@ubuntu2004>
[->] Date: Sun, 18 Apr 2021 08:05:14 +0000
[->] Subject: This is Subject Line
[->] 
[->] Email content line 1
[->] Email content line 2
[->] 
[->] .
[<-] 250 OK id=1lY2Qe-003yGr-RV [->] QUIT
[<-] 221 box2419.bluehost.com closing connection

You can now check your inbox to check your email.

You can also send an email using the mail command. To do so, you will need to install the mailutils package to send an email using the command line. You can install it with the following command:

apt-get install mailutils -y

Once installed, run the following command to send an email with your desired body text and subject line, as shown below:

echo "Your Email Body" | mail -s "your subject" [email protected]

Conclusion

In the above guide, you learned how to install and use SSMTP to send an email from the command line. You can now integrate SSMPT with your application to send an email – try it on your Atlantic.Net VPS today!