msmtp

I was setting up fail2ban and wanted to send related notification emails. Then I found out DigitalOcean blocks smtp ports. So I used Postfix as a Mailgun relay. Then I decided I wanted something more lightweight. Then I found out ssmtp was no longer maintained. Then I found msmtp. So here we go.

Steps n Shit

Refresh package lists

sudo apt update

Ensure ca-certificates is installed

dpkg -l | grep  ca-certificates

Install msmtp

sudo apt install msmtp

Create config file

sudo nano /etc/msmtprc
defaults
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log

account mailgun
host smtp.mailgun.org
port 587
auth on
user YOUR_USER@DOMAIN.TLD
password YOUR_PASSWORD
from FROM@DOMAIN.TLD

account default : mailgun

Set permissions

sudo chmod 644 /etc/msmtprc

create logfile

sudo touch /var/log/msmtp.log
sudo chmod 600 /var/log/msmtp.log

Test

sudo echo "Hello World" | msmtp -d your@realemail.address

Additional Options

Alias sendmail to msmtp

sudo apt install msmtp-mta

This creates a symlink. Verify:

ls -l /usr/sbin/ | grep sendmail
# lrwxrwxrwx 1 root root       12 Feb 17  2016 sendmail -> ../bin/msmtp

Set Up Email Aliases

This will allow cronjobs to send emails to intended targets

sudo nano /etc/aliases

# Send root to user
root: user@domain.com

# Send everything else to other
default: other@domain.org

sudo nano /etc/msmtprc

[...]
aliases               /etc/aliases

Test it

sudo echo "Hello World" | msmtp -d root

Password Management

It’s generally not the best idea to store passwords in plaintext as shown in the config above. This can be resolved using the system keychain or libsecret or GnuPG or other such things. Notes coming eventually maybe likely probs not.

References/Resources

Further Action/Reading

password management