Here’s a quick way to setup SMTP client for emailing logs, reports and errors from a server. This is especially useful for unattended-upgrades or watchtower (for updating Docker containers). I’m using msmtp, which has fairly good support for sendmail and thus integrates well with many applications using native mail in Linux.

  1. Install msmtp and msmtp-mta:
sudo apt install msmtp msmtp-mta

The latter provides alias for sendmail

  1. Copy example system-wide configuration file (provided with the package):
sudo cp /usr/share/doc/msmtp/examples/msmtprc-system.example /etc/msmtprc
  1. Edit the config file:
sudo vim /etc/msmtprc

Here’s an example using gmail:

Your username is just the first part of the email, without domain. If you are using MFA, then you will need to create an application password first.

defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log

account default
host smtp.gmail.com
port 587
from [email protected]
user YOUR_USERNAME
password YOUR_PASSWORD

# Construct envelope-from addresses of the form "[email protected]"
#auto_from on
#maildomain oursite.example

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER
syslog LOG_MAIL
  1. Correct the permissions. Config file must be owned by msmtp group:
sudo chown root:msmtp /etc/msmtprc
sudo chmod 640 /etc/msmtprc
  1. Test it (as root, or a service account relevant in the setup). Type the command, hit enter, type the body of the message and press CTRL + D to send it:
sendmail DESTINATION_EMAIL@ADDRESS

For more info see: https://wiki.archlinux.org/index.php/Msmtp