Jay Taylor's notes
back to listing indexInstall msmtp for forwarding system emails on Debian - TechLabs
[web search]Install msmtp for forwarding system emails on Debian
*UPDATED OCT 2023* How to set up msmtp on Debian 11 for forwarding system emails to an SMTP server or email provider (Office 365, Gmail, SendGrid)
- 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
Added an example for sending emails using msmtp with SendGrid
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
12345mailer-daemon: email@domain.compostmaster: email@domain.comroot: email@domain.comdefault: 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.
12345678910111213# Set default values for all accountsdefaultsport 25tls offaccount smtphost IP-ADDRESSfrom EMAIL@DOMAIN.COMauth off# Set a default accountaccount default : smtp# Aliases filealiases /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
123456789101112131415account o365host smtp.office365.comport 587tls ontls_starttls ontls_trust_file /etc/ssl/certs/ca-certificates.crtauth onfrom EMAIL@DOMAIN.COMuser EMAIL@DOMAIN.COMpassword SECRET# Set a default accountaccount default : o365# Aliases filealiases /etc/aliases
Send email using msmtp with SendGrid
123456789101112131415account sendgridhost smtp.sendgrid.netport 587tls ontls_starttls ontls_trust_file /etc/ssl/certs/ca-certificates.crtauth onfrom EMAIL@DOMAIN.COMuser apikeypassword SECRET-KEY# Set a default accountaccount default : sendgrid# Aliases filealiases /etc/aliases
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
Related Posts
Comments (2)
We have to add set_from_header on in /etc/msmtprc to work for Cron jobs notifications.
- Bold
- Italic
- Underline
- ---------------
- Hyperlink
- Picture
- ---------------
- Bulleted list
- Numeric list
- List item
- ---------------
- Quotes
- Clean
- ---------------
- Happy
- Smile
- Surprised
- Tongue
- Unhappy
- Wink