I used to sift trough my shell history and bookmarks every time set up sending email on a new Debian or Ubuntu server. This should help everybody who just wants to send email trough an old school ISP or free email service.

Be aware don't use ssmtp anymore. It's unmaintained and has been removed from Debian and Ubuntu will most definitely follow suit.

Install msmtp

First we need the awesome program called msmtp to route all the server's mail through a standard SMTP server.

sudo apt-get install msmtp msmtp-mta mailutils

Set up msmtp

sudo nano /etc/msmtprc

# Set default values for all following accounts.

# Use the mail submission port 587 instead of the SMTP port 25.
port 587

# Always use TLS.
tls on

# Set a list of trusted CAs for TLS. The default is to use system settings, but
# you can select your own file.
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# The SMTP server of your ISP
account isp
host mail.isp.example
from [email protected]
auth on
user 12345

# Set default account to isp
account default: isp

# Map local users to mail addresses
aliases /etc/aliases

The above is based on the example. The program has many more authentication methods.

Install and set up mailx

In order to be able to use the mail command wee need to install mailx

sudo apt-get install bsd-mailx

Set mail transport agent to use msmtp

sudo nano /etc/mail.rc

append the following:

set mta=/usr/bin/msmtp

Set up aliases

We need to link system users with email addresses in order for system users to receive mails from cronjobs.

sudo nano /etc/aliases

# Send root to Jane
root: [email protected]

# Send everything else to admin
default: [email protected]

sudo nano /etc/mail.rc


alias root root<[email protected]>

Emails are now sent to this address if e.g. a cronjob fails. Also a general fallback address is used if messages don't belong to root. Of course more users can be set.

Test it!

echo "Hello World" | msmtp -d [email protected]

Test sending a mail to root

echo "Testing msmtp from ${HOSTNAME} with mail command" | mail -s "hi root" root

Test sending a mail to another email adress

echo "Testing msmtp from ${HOSTNAME} with mail command" | mail -s "hi there" [email protected]