How to set up MSMTP on Debian to use a mailhoster with SMTP

Latest update: Jul 2022

Steps


# First we need to install packages:

sudo apt install msmtp mailutils

Note: mailutils is needed for sending mail via “mail” on the command line.

# Edit the global configuration file:

sudo nano /etc/msmtprc
# Set default values for all accounts.
account default
auth            on
tls             on
tls_trust_file  /etc/ssl/certs/ca-certificates.crt

# netcup
host            <yourMailHost>
port            587
from            <mail@domain.tld>
user            <user>
password        <pass>

aliases         /etc/msmtp-aliases

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER
syslog LOG_MAIL
# Create the aliases file:

sudo nano /etc/msmtp-aliases

# and fill it with:

# Send root mails to
root: <yourAddress>

# Send cron mails to
cron: <yourAddress>

# Send everything else to admin
default: <yourAddress>
# Create the mail.rc. file:

sudo nano /etc/mail.rc

# and fill it with:

set sendmail="/usr/bin/msmtp -t"

Troubleshooting

# If you need to check the logs in case of troubleshooting:

sudo tail -f /var/log/mail.log

Credits