Ubuntu — Send Mail with msmtp
msmtp is a lightweight SMTP client that lets your server send email — useful for cron job failures, script alerts, and unattended-upgrades notifications. It is actively maintained and available in the standard Ubuntu repositories.
Pre-requisites
- Ubuntu 22.04+ server
- An SMTP account to send through (your mail server, Gmail, Postmark, etc.)
sudoaccess
Install
msmtp-mta sets msmtp as the system MTA, so tools like mail and cron use it automatically.
Configure
msmtp uses two config files depending on the sending user:
| File | Used by |
|---|---|
/etc/msmtprc |
root and system services |
~/.msmtprc |
Non-root users |
System-wide config (/etc/msmtprc)
# Defaults for all accounts
defaults
port 587
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# Your mail account
account myserver
host mail.example.com
from server@example.com
auth on
user server@example.com
password YourPassword
# Set this account as default
account default : myserver
Replace mail.example.com, server@example.com, and YourPassword with your actual SMTP credentials.
Restrict read access — the file contains a plaintext password:
Configure Aliases
Aliases define where system mail (cron errors, failed services) gets delivered. Edit /etc/aliases:
This routes all system mail to your address.
Test
Send a test message:
Check the log if the mail does not arrive (logging is not enabled by default — add this to /etc/msmtprc to enable it):
Then tail the log after sending:
Common Issues
| Symptom | Cause | Fix |
|---|---|---|
cannot open mail.example.com:587 |
Wrong host or port | Verify SMTP server address and port |
TLS handshake failed |
Certificate mismatch or TLS issue | Check tls_trust_file path, try port 465 with tls_starttls off |
authentication failed |
Wrong credentials | Double-check username and password; some providers require an app password |
| No mail received, no error | Aliases not configured | Check /etc/aliases and run newaliases if needed |
| Cron jobs don't send mail | msmtp-mta not installed |
sudo apt install msmtp-mta |
App passwords
If you use Gmail or Microsoft 365, you likely need to generate an app-specific password. Account passwords are often blocked for SMTP auth by default.