Atlantic.Net Blog

How to Use SSMTP to Send an Email from Linux Terminal

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 the external email address.

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

Prerequisites

  • A fresh Ubuntu 20.04 server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 20.04 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Install SSMTP

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

apt-get install ssmtp -y

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

Step 3 – 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 use your SMTP server address to send an email.

Step 4 – 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 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:<[email protected]>
[<-] 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" <[email protected]>
[->] 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 the 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!

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year