Send E-Mail from Terminal
Sending emails directly from the Linux terminal can be a powerful and efficient way to manage communication, automate tasks, and monitor system activities. The ability to send an email from the command line removes the need for a full-fledged email client and allows for streamlined processes in automated scripts or systems administration.
There are many scenarios where terminal-based email functionality becomes crucial:
- Automating Notifications: Send alerts when a scheduled task finishes, when a threshold is reached (e.g., disk space usage, CPU usage, number of sessions ), or if a system failure occurs.
- Script Integration: Automate reporting processes, like sending log files, system status reports, or backups via email without manual intervention.
- Remote Administration: For remote Linux servers, sending email directly from the terminal allows you to quickly communicate with administrators or users from within scripts or cron jobs.
- Efficiency: The command line provides a fast and resource-efficient way to send email without needing a graphical interface or third-party applications.
Introduction
Send e-mail 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 on Linux
Step 1: Install ssmtp
and mailtuils
to send E-Mail
Before sending emails from the command line, install the required packages: ssmtp and mailutils. These tools provide a straightforward way to set up and manage email delivery on your Linux system.
To install the packages, run the following commands:
sudo apt install ssmtp
sudo apt install mailutils
ssmtp handles the email sending via an SMTP server, while mailutils provides additional tools for mail operations.
Step 2: Put your Parameters in ssmtp
Configuration
To configure ssmtp open the configuration file in a text editor:
sudo nano /etc/ssmtp/ssmtp.conf
You need to edit /etc/ssmtp/ssmtp.conf
the file and put your e-mail address, mail server configuration and other parameters.
#
# 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
In this example:
mailhub
: The address of the SMTP server.AuthUser
: Your email address.AuthPass
: Your email password (or App password if 2FA is enabled).UseTLS
andUseSTARTTLS
: Ensures secure encrypted communication with the SMTP server
Send Test E-Mail from Terminal
To send an e-mail from terminal, you can use this command:
echo "Body of your email" | mail -s "Test E-Mail Subject" user@mail.com
Check out More Similar Guides
If you found this guide on sending emails from the Linux terminal helpful, you might also like these related tutorials on Linux system optimization and automation:
- How to Monitor Disk Usage on Linux with Email Alerts Using a Bash Script and Cron
Learn how to set up email alerts for disk usage on your Linux server using Bash scripts and Cron jobs. - How to Monitor Disk Usage on Linux and Automatically Receive Email Alerts
This guide will show you how to automatically monitor disk usage and get email notifications when your disk space exceeds a certain threshold.
Explore these and other useful tutorials on DefenceDev for more tips and tricks on managing your Linux system!