How to set up email notifications for cron jobs on Debian 11 (Bullseye) using the msmtp SMTP email client.
Install msmtp and msmtp-mta
apt-get install msmtp msmtp-mta
/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
sudo nano /etc/msmtprc
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
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/msmtprcsudo 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
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 1
Hello,
it seems this is not working anymore, with new microsoft rules from 16 september
Can configuration be changed to keep using a @outlook email ?
535 5.7.139 Authentication unsuccessful, basic authentication is disabled.