Introduction
Send e-mail directly from the Linux terminal can be a powerful tool for automating system alerts and sending quick notifications. I use this approach for various checks and reports that are generated on my Linux machines. I put the email sending scripts in a crontab and in this way automate sending reports and notifications that I have previously prepared.
In this guide, we’ll walk you through the steps to configure and send emails from the terminal using various utilities like mailutils
and ssmtp
.
By the end, you’ll be able to integrate email functionality into your scripts.
Install SSMTP Application
Step 1: Install ssmtp
and mailtuils
sudo apt install ssmtp
sudo apt install mailutils
Step 2: Put your Paremeters in ssmtp
Configuration
You need to edit /etc/ssmtp/ssmtp.conf
file and put your e-mail address, mail server configuration and other paramters.
sudo nano /etc/ssmtp/ssmtp.conf
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
Root=user@mail.com
#FromLineOverride=YES
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail.server.com:port
# Use TLS encryption
UseSTARTTLS=YES
# Where will the mail seem to come from?
rewriteDomain=mail.com
# The full hostname
hostname=HOSTNAME
AuthUser=user@mail.com
AuthPass=password
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES
Send Test E-Mail from Terminal
To send an e-mail from from terminal you can use this command:
echo "Body of your email" | mail -s "Test E-Mail Subject" user@mail.com
Other Useful Examples
Check the example how to send mail from terminal (integration in script) on this link.