Linux: How to Send Mail From Command Line Using SMTP Server [Complete Guide]

Published on 2019-09-20· Updated on 2024-01-25

The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.

Introduction

In this tutorial, you will learn how to send emails from a Linux operating system's terminal or shell script using popular CLI tools.

This tutorial will help you send critical server-level emails, such as Cron reports, script logs, customer registrations, receipts, and bank statements. 

With simple libraries and very few configurations, you can have a CLI tool in your Linux OS that you can use to send mail from the terminal.

Prerequisites

  • Linux operating system
  • SMTP Configurations (SMTP server details and authentication credentials)
  • Your favorite editor (Optional)
  • Configure Gmail SMTP/ready with any other custom SMTP server details. This tutorial talks about using Gmail SMTP to send emails.
  • Google SMTP server configurations would look something like this:
  • SMTP Server/Hostname: smtp.gmail.com
  • SMTP Username: [Your Gmail Address]
  • SMTP password: [Your Gmail Password]
  • SMTP Port: 587
  • TLS/SSL: Required

It's time to open the terminal

Here’s how you use sSMTP to send emails from your Linux command line:  sSMTP is a lightweight utility that allows users to send emails from the command line or shell scripts. This makes it particularly useful for headless servers or cloud platforms without a full mail server installation.

How to install sSMTP to send mails from your Linux command line (CLI)

Step 1

Use the below command to install ssmtp:

sudo apt-get install ssmtp

Optional:
CentOS users can use the below command to install ssmtp:

sudo yum install ssmtp

In CentOS, you may encounter the error "package sSMTP is not available" during installation. To resolve this issue, use the following command:

sudo yum --enablerepo=extras install epel-release

Step 2

After successfully installing sSMTP, configure the global settings as given below to enable mail sending. Open the specified file using your favorite editor, such as nano or vi:

sudo vim /etc/ssmtp/ssmtp.conf

Use the Gmail SMTP server to update the above file with the following details.

mailhub=smtp.gmail.com:587
useSTARTTLS=YES
AuthUser=username-here
AuthPass=password-here
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt

To use a third-party SMTP, enter its hostname as the mail hub parameter. For instance, if you want to use Netcore Email SMTP instead of smtp.gmail.com, enter smtp.pepipost.com.

mailhub: your smtp server host/ip with port.

UseSTARTTLS: Set it to Yes if the SMTP server uses TLS or else No.

AuthUser: Use Gmail ID 

AuthPass: Use Gmail ID’s password

TLS_CA_File: This may be required sometimes if you face an issue like ‘send-mail: Cannot open smtp.gmail.com:587’

You are now prepared to send emails using the command line (CLI).

Step 3

There are various methods to use the sSMTP command to send emails.

Case 1: Send Mail Directly From The Command Line

Copy and paste the command below to send an email from your command line.

echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv [email protected]

-vvv is the verbosity to see the logs while sending the mail

Case 2: Send Mail From A Shell Script

You can also use sSMTP to send mail from a shell script. To do this, open your preferred editor and create a shell script file called 'saymail.sh' and copy and paste the following code:

#!/bin/sh  
SUBJECT="Test Subject"
TO="[email protected]"
MESSAGE="Hey There! This is a test mail"

echo $MESSAGE | sudo ssmtp -vvv $TO

Ensure proper permission access is set for your script file. Use this command to set permission:

$ sudo chmod 755 mail.sh 

Now, execute the code by running the shell script with the following command:

$ sudo ./mail.sh

Here are some common errors/exceptions you may encounter when sending mail with sSMTP:

Error 1

You may encounter the below error message while attempting to send an email:

sSMTP: Authorization failed (535 5.7.8 https://support.google.com/mail/?p=BadCredentials u65smyez14952a76922r5pfui.104 - gsmtp)

[<-] 220 smtp.gmail.com ESMTP u65sm14952769pfu.104 - gsmtp
[->] EHLO kali
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO kali
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5edhbAWU6
[->] dmlzaGFsY2hhasd2dWhhbjIyMTJAZ21haWwuY29t
[<-] 334 EUEeGFzc3dvfcaqQ6
[<-] 535 5.7.8  https://support.google.com/mail/?p=BadCredentials u65smyez14952a76922r5pfui.104 - gsmtp
ssmtp: Authorization failed (535 5.7.8  https://support.google.com/mail/?p=BadCredentials u65smyez14952a76922r5pfui.104 - gsmtp)
crazy@kali:~/Documents/Scripts$ 

In such case, try the following solutions: 

1. Enable ‘Allow less secure app’ in Google account settings. 

2. Verify correct login credentials. 

3. Run the shell script again for successful output.

$ sudo ./mail.sh

Output:

[<-] 220 smtp.gmail.com ESMTP h8sm1096aae22880pfo.64 - gsmtp
[->] EHLO kali
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO kali
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5edhbAWU6
[->] dmlzaGFsY2hhasd2dWhhbjIyMTJAZ21haWwuY29t
[<-] 334 UGFzqc3dmvcmQ36
[<-] 235 2.7.0 Accepted
[->] MAIL FROM:<root@kali>
[<-] 250 2.1.0 OK h8smqer10962480pfo.64 - gsmtp
[->] RCPT TO:<[email protected]>
[<-] 250 2.1.5 OK h8smqer10962480pfo.64 - gsmtp
[->] DATA
[<-] 354  Go ahead h8sm10962880pfo.64 - gsmtp
[->] Received: by kali (sSMTP sendmail emulation); Thu, 19 Sep 2019 21:45:14 +0530
[->] From: "root" <root@kali>
[->] Date: Thu, 19 Sep 2019 21:45:14 +0530
[->] Hey There! This is a test mail
[->] 
[->] .
[<-] 250 2.0.0 OK  1568909725 h8smqer10962480pfo.64 - gsmtp
[->] QUIT
[<-] 221 2.0.0 closing connection h8smqer10962480pfo.64 - gsmtp

To sum up, sSMTP is a useful tool for sending emails from the Linux command line. It is particularly helpful for headless servers or cloud platforms that do not have a full mail server setup. By following the above steps l, you can send important server-level emails with ease, from installation to configuration and sending. 

We are a community of email enthusiasts. If you have further queries, feel free to drop an email to [email protected] , and we would be happy to help you.

Excited about the latest in Bulk Email Marketing! Check out this insightful blog on Gmail and Yahoo updates in the email marketing landscape.
Explore the Blog - Here
Stay ahead of the game with valuable insights on optimizing your email campaigns! 📬

<!-- Happy Coding />

Grade My Email
Check your spam now?

Netcorecloud's toolkit is the solution to all your email problems.

Vishal Chauhan

You can also explore

Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.

Deploy emails that are
screenshot worthy!

Stop settling for static emails - use Netcore's platform to create dynamic, interactive emails that convert.
Let's Deploy
Get Started Now