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.
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
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
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
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.
msmtp - main packagemsmtp-mta - package that creates sendmail aliasmailutils - gnu mail utilities package.
mailutils package?