Install msmtp for forwarding system emails on Debian
How to set up msmtp on Debian 11 for forwarding system emails to an SMTP server or email provider (Office 365, Gmail)
- 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
Install msmtp and msmtp-mta
apt-get install msmtp msmtp-mta
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 aliases 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
Change the permissions of /etc/msmtprc
Change the permissions of /etc/msmtprc so that owner (root) can read/write and group (msmtp) can read the 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 rootecho "Testing root email alias"| msmtp root
-v print debugging information for troubleshootingecho "Testing root email alias"| msmtp -v root
References:
msmtp documentation
https://marlam.de/msmtp/documentation
Debian wiki msmtp
https://wiki.debian.org/msmtp
Debian manpages msmtp
https://manpages.debian.org/bullseye/msmtp-mta/sendmail.8.en.html
Comments