Setup Email Notifications for Cron Jobs on Debian
How to set up email notifications for cron jobs on Debian 11 (Bullseye) using the msmtp SMTP email client.
Install msmtp
Install msmtp and msmtp-mta
apt-get install msmtp msmtp-mta
- msmtp is light SMTP email client that can be easily configured to forward emails to an SMTP server/email provider
- msmtp supports server profiles that can be used for sending mail to different email providers
- msmtp-mta creates an alias for sendmail, this means it can be used with any system that requires sendmail
Create an aliases file
/etc/aliases is used by sendmail to redirect messages sent to local users/recipients to an email address
mailer-daemon - used for system messages about email issues
postmaster - the admin person who looks after email issues
root - redirect any emails sent to the root user
default - send everything else to this address
sudo nano /etc/aliases
mailer-daemon: email@domain.com postmaster: email@domain.com root: email@domain.com default: email@domain.com
Rebuild alias database
newaliases
Configure msmtp
sudo nano /etc/msmtprc
Send email using msmtp without authentication
This example creates a profile called smtp that will be used to send mail to an internal SMTP relay server that doesn't require authentication. The smtp profile is set as the default account.
# Set default values for all accounts defaults port 25 tls off account smtp host IP-ADDRESS from EMAIL@DOMAIN.COM auth off # Set a default account account default : smtp # Aliases file aliases /etc/aliases
Send email using msmtp to Office 365
This example creates a profile called o365 that will be used to send mail using Office 365
account o365 host smtp.office365.com port 587 tls on tls_starttls on tls_trust_file /etc/ssl/certs/ca-certificates.crt auth on from EMAIL@DOMAIN.COM user EMAIL@DOMAIN.COM password SECRET # Set a default account account default : o365 # Aliases file
sudo chown root:msmtp /etc/msmtprc
sudo chmod 640 /etc/msmtprc
Send a test email
Send a test email to an external address
echo "Test email using msmtp" | msmtp EMAIL@DOMAIN.COM
Send a test email to root
echo "Testing root email alias"| msmtp root
-v print debugging information for troubleshooting
echo "Testing root email alias"| msmtp -v root
Edit crontab
sudo crontab -e
Add this line at the top of the file to get email notifications from cron
MAILTO=EMAIL@DOMAIN.COM
References:
msmtp documentation
https://marlam.de/msmtp/documentationDebian wiki msmtp
https://wiki.debian.org/msmtpDebian manpages msmtp
by Author
https://manpages.debian.org/testing/msmtp/msmtp.1.en.html
Comments