How to Send Mail From Command Line Using SMTP Server in Linux.

SSMTP is a program which allow to send email to a designed mailhost (mailhub). It accepts a mail stream on standard input with recipients on the command line.We can send mail using SSMTP program & failed messages are placed in dead.letter in the sender’s home directory.

SSMTP delivers emails from a user’ system or a server to a configured mail host. SSMTP is not an mail server and does not receive emails or manage a queue.

Prerequisites

  • SMTP Configurations (SMTP server details and authentication credentials)
  • Configure Gmail SMTP/ready with any other custom SMTP server details.

Google SMTP server configurations:

  • SMTP Server/Hostname: smtp.gmail.com
  • SMTP Username:[email protected]
  • SMTP password: password
  • SMTP Port: 587
  • TLS/SSL: Required

Update the System.

apt-get update

Install SSMTP

apt-get install ssmtp -y

Configure sSMTP.

vim /etc/ssmtp/ssmtp.conf

Add the following line in the ssmtp.conf file.

hostname=system-host-name
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
[email protected]
AuthPass=password
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt

Add reverse aliases

vim /etc/ssmtp/revaliases

Add the line:

root:[email protected]:smtp.gmail.com:587

Create a message to send.

vim msg.txt

Add the following message.

To: [email protected]
From: [email protected]
Subject: Example email

Testing!

Send mail.

/usr/sbin/ssmtp [email protected] < msg.txt

Sendmail in a single line.

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

ssmtp to send mail from a shell script.Create a shell script file.

vim mail.sh

Add the following line.

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

echo $MESSAGE | sudo ssmtp -vvv $TO

Set the permission.

chmod 755 mail.sh 

Run the script.

bash mail.sh
or
sudo ./mail.sh

 

 

2 responses to “How to Send Mail From Command Line Using SMTP Server in Linux.”

    • Hi,

      Thanks for your update.

      This is basic blog and you are most welcome if you have something to add.

      Just register yourself and add the same, we would love to read it.

Leave a Reply

Your email address will not be published.