Linux server email relay for Microsoft 365 and Gmail
Many tools on a Linux server send email through the server’s own mail system (the sendmail command), rather than connecting to an email provider themselves: cron job output, Nagios and Icinga alerts, fail2ban, disk and RAID warnings from smartd and mdadm, logwatch reports, unattended upgrades, and many backup scripts. To deliver this email, the server relays it through a real mailbox, signing in with a username and password – which Microsoft 365 and Gmail don’t accept, as they require OAuth.
With Auth-Email, you set up the relay once, and every tool on the server can use it. Auth-Email signs in to your mailbox with OAuth on the relay’s behalf.
This guide covers two common ways to set up the relay:
- msmtp: a small, send-only relay. The simplest choice for servers that only need to send email
- Postfix: a full mail server that queues and retries email if it can’t be sent straight away. Use this if Postfix is already installed, or if you need queuing
Before you start: link the mailbox in your Auth-Email dashboard, and choose a password for it. That is the password you’ll enter in the relay’s configuration – not your mailbox password.
The examples below use an Outlook account. Show the server and port for an account provided by Outlook: use outlook.auth-email.com with port 465 wherever the examples show outlook.auth-email.com and 465.
Option 1: msmtp (simplest)
Install msmtp, with the package that lets it replace the sendmail command (on Debian and Ubuntu):
sudo apt install msmtp msmtp-mta
On Red Hat, Rocky, AlmaLinux, and Fedora, install msmtp (from EPEL if needed), and link it as sendmail. Then create /etc/msmtprc:
defaults
auth on
tls on
tls_starttls off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
syslog on
account auth-email
host outlook.auth-email.com
port 465
user alerts@yourdomain.com
password your-auth-email-password
from alerts@yourdomain.com
set_from_header on
account default : auth-email
aliases /etc/aliases
- tls_starttls off connects using SSL/TLS straight away, as Auth-Email expects on port 465
- from and setfromheader on make every message come from the linked mailbox, whichever program or user sends it. Without these, messages from
root@yourserverare rejected. (set_from_headerneeds msmtp 1.8.8 or later – check withmsmtp --version) - aliases lets you choose where email for local users goes (see choose where local email goes)
The file contains a password, so only let the system read it: sudo chmod 600 /etc/msmtprc. (On Debian and Ubuntu, use sudo chown root:msmtp /etc/msmtprc and sudo chmod 640 /etc/msmtprc instead, so that non-root users can still send email.)
Option 2: Postfix
Install Postfix and the SASL authentication modules. On Debian and Ubuntu, choose Satellite system if you’re asked how to configure Postfix:
sudo apt install postfix libsasl2-modules # Debian and Ubuntu
sudo dnf install postfix cyrus-sasl-plain # Red Hat, Rocky, AlmaLinux, and Fedora
Add (or change) the following settings in /etc/postfix/main.cf:
relayhost = [outlook.auth-email.com]:465
smtp_tls_wrappermode = yes
smtp_tls_security_level = encrypt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = texthash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
sender_canonical_maps = regexp:/etc/postfix/sender_canonical
inet_interfaces = loopback-only
Create /etc/postfix/sasl_passwd, with the linked mailbox’s address and the password you chose in Auth-Email:
[outlook.auth-email.com]:465 alerts@yourdomain.com:your-auth-email-password
Create /etc/postfix/sender_canonical, so that every message comes from the linked mailbox, whichever program or user sends it:
/.+/ alerts@yourdomain.com
Then protect the password file, and restart Postfix:
sudo chmod 600 /etc/postfix/sasl_passwd
sudo systemctl restart postfix
- smtptlswrappermode connects using SSL/TLS straight away, as Auth-Email expects on port 465
- inet_interfaces = loopback-only only accepts email from programs on the server itself, so the server can’t be used as an open relay
- If you change
sasl_passwdlater, restart Postfix again
Choose where local email goes
Many tools send email to local users such as root, rather than to a real address. To receive it, add a line to /etc/aliases that forwards it to you:
root: you@yourdomain.com
default: you@yourdomain.com
With Postfix, run sudo newaliases afterwards. (The default line is only used by msmtp, for any other local user.) For cron jobs, you can also set MAILTO=you@yourdomain.com at the top of a crontab.
Send a test
printf "Subject: Test from $(hostname)\n\nIt works!\n" | sendmail root
You should receive the message at the address you set for root within a few seconds. If you don’t, check the mail log: sudo journalctl -t msmtp for msmtp, or sudo journalctl -u postfix (or /var/log/mail.log) for Postfix.
Troubleshooting
- “535 Authentication failed” or “SASL authentication failed”: check that the username is the linked mailbox’s full email address, and that the password is the one you chose in Auth-Email (not your mailbox password). With Postfix, check that
libsasl2-modules(orcyrus-sasl-plain) is installed – without it, Postfix logs “no mechanism available” - The connection hangs, or “wrong version number” / TLS errors: check that msmtp has
tls_starttls off, or that Postfix hassmtp_tls_wrappermode = yes, when using port 465 “Sender address rejected” or “SendAsDenied”: a message was sent from an address that the linked mailbox isn’t allowed to use. Check the
fromandset_from_headerlines (msmtp), or thesender_canonicalfile (Postfix)Connection times out: some hosting providers block outgoing email ports. Check that your server can connect out on port 465 – for example, with our SMTP connection tester
- Postfix has queued messages from before the fix: run
sudo postqueue -fto try sending them again, orsudo postsuper -d ALLto delete them
For other errors, such as Microsoft 365 reporting that SMTP AUTH is disabled, see fixing common errors. Your account’s connection page in the Auth-Email dashboard also shows whether your server has connected, and explains any error it received. Still stuck? Email us – we’ll help you get it working.
Related articles
Try Auth-Email with your servers
Link one mailbox, point msmtp or Postfix at Auth-Email, and send a test message from the command line.
Start your free trial →No credit card needed. Check that it works, including sending yourself test messages, before you pay.