Send a Message with mail
echo "Backup finished" | mail -s "Server report" YOUR_EMAIL
-s sets the subject and the piped text is the body. Replace YOUR_EMAIL with the recipient address. Install the command first:
sudo apt install mailutils # Debian and Ubuntu
sudo dnf install s-nail # AlmaLinux and Rocky Linux
The mail command hands the message to a local mail transfer agent such as Postfix. The package manager may offer to install one; without it, or without a relay, the message goes nowhere.
Will the Mail Arrive?
- Port 25 is often blocked: many providers block outbound connections on port 25 to stop spam, so direct delivery from the server fails.
- Spam folders: receiving servers check SPF, DKIM, and the reverse DNS record of the sending IP. Mail from a server without them, or with a reverse DNS name that does not match its hostname, often lands in spam or is rejected.
- The reliable route: send through an authenticated SMTP service on port 587, which already has those records in place for its domain.
Use sendmail Directly
Postfix, Exim, and msmtp all provide a sendmail command. It reads the headers and the body from standard input:
printf "Subject: Disk check\n\nAll disks below 80%%.\n" | sendmail YOUR_EMAIL
A blank line separates the headers from the body.
Relay Through an Authenticated SMTP Service with msmtp
msmtp is a small client that passes every message to one SMTP server. Install it with sudo apt install msmtp msmtp-mta on Debian and Ubuntu; on AlmaLinux and Rocky Linux it comes from the EPEL repository. Keep the password out of the command line and out of the shell history: open a new file in an editor, write only the password in it (shown here as STRONG_PASSWORD), and protect it:
nano ~/.smtp-password
chmod 600 ~/.smtp-password
Then create ~/.msmtprc and give it the same permissions. Replace YOUR_SMTP_USER with the relay's login name:
defaults
auth on
tls on
logfile ~/.msmtp.log
account relay
host smtp.example.com
port 587
from YOUR_EMAIL
user YOUR_SMTP_USER
passwordeval "cat ~/.smtp-password"
account default : relay
chmod 600 ~/.msmtprc
printf "Subject: Relay test\n\nHello from the server.\n" | msmtp YOUR_EMAIL
msmtp checks the relay's certificate against the system's trusted certificates by default, so no certificate path is needed on either family. passwordeval runs the command and uses its output as the password. With msmtp-mta installed on Debian and Ubuntu, the mail and sendmail commands send through the same relay.
Test the Connection with swaks
swaks prints the whole SMTP conversation, which shows exactly where delivery fails. Install it with sudo apt install swaks, or from EPEL on the Red Hat family. Test the local mail server:
swaks --to YOUR_EMAIL --server localhost
Test a relay with TLS and a login. Leave the password out of the command; swaks asks for it:
swaks --to YOUR_EMAIL --from YOUR_EMAIL --server smtp.example.com:587 --tls --auth --auth-user YOUR_SMTP_USER
Send an Attachment with mutt
echo "Report attached" | mutt -s "Daily report" -a /var/backups/report.csv -- YOUR_EMAIL
Install mutt with apt or dnf first. -a must come after the other options, and the -- ends the list of attachments. The mailutils version of mail attaches with -A, and s-nail with -a.
Use It in Scripts and Cron
df -h | mail -s "Disk report" YOUR_EMAIL
Any command's output can be piped into mail. In a crontab, a line MAILTO=YOUR_EMAIL sends the output of every job to that address. Writing the script itself is covered in how to write and run a Bash script, and a disk report in how to check disk space in Linux.
When Mail Does Not Arrive
- Read the log:
sudo journalctl -u 'postfix*', the file/var/log/mail.logon Debian and Ubuntu systems that run rsyslog, or~/.msmtp.log. tail -f follows it live. - Check the queue:
mailqlists messages Postfix could not deliver yet. - Check DNS:
dig TXT example.com +shortshows the SPF record, anddig -x 203.0.113.10 +shortthe reverse DNS name.