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:sender@gmail.com
  • 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
AuthUser=@gmail.com
AuthPass=password
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt

Add reverse aliases

vim /etc/ssmtp/revaliases

Add the line:

root:SenderEmailAddress@gmail.com:smtp.gmail.com:587

Create a message to send.

vim msg.txt

Add the following message.

To: ReceiverEmailAddress@gmail.com
From: SenderEmailAddress@gmail.com
Subject: Example email

Testing!

Send mail.

/usr/sbin/ssmtp receiver@email.com < msg.txt

Sendmail in a single line.

echo "Test message using ssmtp" | sudo ssmtp -vvv revceiver-email@gmail.com

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="receiver@email.com"
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