How to Send Emails from the Linux Command Line
Sending emails directly from the Linux command line is a fundamental skill for system administrators, developers, and anyone managing server-side operations. Whether you need to deliver automated alerts from cron jobs, forward script output to an inbox, or send notifications from a custom application, command-line email tools provide a powerful and flexible solution. This tutorial guides you through setting up and using three essential tools: mail (or mailx) for basic text emails, mutt for handling attachments reliably, and msmtp for connecting to external SMTP services that require authentication.
Setting Up Your Email Environment
Before you can send emails, you need to install the necessary packages on your Linux system. The specific commands vary slightly between Debian/Ubuntu-based distributions and RHEL-family systems like AlmaLinux or Rocky Linux.
For Debian/Ubuntu Systems
On Debian and Ubuntu, the primary tool for basic command-line email is provided by the mailutils package. This package often installs or depends on a Mail Transfer Agent (MTA) like Postfix to handle the actual sending of emails.
Update your package list:
sudo apt updateInstall
mailutils:sudo apt install mailutils -yDuring the installation, if Postfix is installed, you might be prompted to configure it. For most simple use cases where you're sending outbound emails from a server, select 'Internet Site'. You can navigate the dialogs using the
Tabkey and confirm withEnter.
For RHEL-Family Systems (AlmaLinux, Rocky Linux)
On RHEL-based distributions, the equivalent package is typically mailx. These systems often use Sendmail or Postfix as their default MTA, which mailx can interface with.
Install
mailx:sudo dnf install mailx -y
Sending Basic Text Emails with mail / mailx
The mail (or mailx) command is perfect for sending quick, plain-text messages. It's simple to use and ideal for automated scripts where you just need to deliver a short notification.
Interactive Mode
You can use mail in an interactive mode, where you type the subject and body directly into the terminal.
Start the
mailcommand, specifying the subject and recipient:mail -s