Jay Taylor's notes

back to listing index

Install msmtp for forwarding system emails on Debian - TechLabs

[web search]
Original source (techlabs.blog)
Tags: linux howto email smtp msmtp notifications techlabs.blog
Clipped on: 2025-02-16

2 minutes reading time (413 words)

Install msmtp for forwarding system emails on Debian

·
Monday, 02 October 2023
· 7711 Hits

*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 
UPDATED OCT 2023
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

1
2
3
4
5
mailer-daemonemail@domain.com
postmasteremail@domain.com
rootemail@domain.com
defaultemail@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.

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
aliases /etc/aliases 

Send email using msmtp with SendGrid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
account sendgrid
host smtp.sendgrid.net
port 587
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
auth on
from EMAIL@DOMAIN.COM
user apikey
password SECRET-KEY
# Set a default account
account default : sendgrid
# Aliases file 
aliases /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 root
echo "Testing root email alias"| msmtp root

-v print debugging information for troubleshooting
echo "Testing root email alias"| msmtp -v root

    
 

Comments (2)

Guest - M. Iqbal on Wednesday, 27 December 2023 13:52

We have to add set_from_header on in /etc/msmtprc to work for Cron jobs notifications.

0 Reply
TechLabs on Wednesday, 07 February 2024 16:23

Thanks for taking the time to comment, it’s much appreciated!

0 Reply
Already Registered? Login Here
Sunday, 16 February 2025
Subscribe to the blog (Please fill in your email address to subscribe to updates from this post.)
Automation
5 post(s)
Azure
29 post(s)
Debian Linux
19 post(s)
Guides
75 post(s)
Intune
13 post(s)
Joomla
10 post(s)
Office 365
59 post(s)
OPNsense
8 post(s)
You can help support this website by buying me a coffee!
© 2025 TechLabs. Designed by TecTribe