Skip to content

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.)
  • sudo access

Install

sudo apt install msmtp msmtp-mta mailutils -y

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)

sudo nano /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:

sudo chmod 600 /etc/msmtprc

Configure Aliases

Aliases define where system mail (cron errors, failed services) gets delivered. Edit /etc/aliases:

sudo nano /etc/aliases
root: admin@yourdomain.com
default: admin@yourdomain.com

This routes all system mail to your address.


Test

Send a test message:

echo "msmtp is working" | mail -s "Test from $(hostname)" admin@yourdomain.com

Check the log if the mail does not arrive (logging is not enabled by default — add this to /etc/msmtprc to enable it):

logfile /var/log/msmtp.log

Then tail the log after sending:

sudo tail -f /var/log/msmtp.log

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.