Setup Postfix Relay Server with Microsoft Exchange Email

Posted January 26, 2021 in Blog, Server

Setup Postfix Relay Server with Microsoft Exchange Email

In this article, you will be guided on how to set up a Postfix relay SMTP-server and configure Microsoft Exchange to handle its mail queue.

In a later article, we will discuss how to set up the relay server as a docker container.

Prerequisites

  • A server or access to create a new server (if you are using DigitalOcean: Droplet)
  • A Microsoft Exchange account. It might work with a free Outlook 365 account, but this article will be based on Microsoft Exchange which costs about $4 per month.
  • Your Exchange email DNS has already been configured (You can send and receive emails on your domain)
  • Access to your applications DNS settings
  • Basic Linux and network skills (It’s mostly copy-paste)

Introduction

Sooner or later any web application must send out emails, however, ensuring the emails are not ending up in the recipient’s spam folder, server IP is blacklisted or other obstacles can either be expensive or time-consuming.

Some solutions offer an API to this where they handle the mail server part for a fee. A lot will have a free as well, however, you will then in both cases be sharing the outgoing IP with many other clients heavily increasing the risk of being assigned a blacklisted one.

The reality for CloudMonitor has been that the low-cost solutions couldn’t tackle our email problem and the higher-priced couldn’t be justified. Searching for other solutions we decided to set up a Postfix relay server that transports the queue to Microsoft Exchange. Some of the benefits of using Microsofts Exchange are:

  • World-leading mail infrastructure
  • Never blacklisted
  • Cheap service

Cons could be:

  • No dashboard or metrics
  • No API

These cons are fine for now as tracking can still be done with Matomo.

Installing Postfix

Since we are not going to handle questions about spam or sender validity, the setup of Postfix will be straight forward.

MX Domain

Before you start you should know the domain of your MX record. You can look up your domain at MX Toolbox to see which hostname is set for it: https://mxtoolbox.com/SuperTool.aspx?action=mx:&run=toolpage

Configure application DNS

Open your Exchange dashboard at https://admin.microsoft.com/

Under domains click Add domain

Exchange Dashboard

Enter the domain name you want to add (The domain of your email sender address).

Add domain to Exchange

Select that you will verify your domain by TXT-record.

How to verify domain

Copy the given details to your DNS settings

Verify your domain DNS

Here shown how it will look in DigitalOcean

DigitalOcean DNS

After your DNS has been added, simply choose to continue on how you want to connect your domain

How to connect domain

Now you will be given your DNS details under MX Records, CNAME Records, and TXT Records. Insert those in your DNS settings.

Add DNS for domain

Your domain is now added to Microsoft Exchange.

Install Postfix

On a newly created Ubuntu server install Postfix:

apt install postfix
postconf -e relayhost=<MX-hostname>
postconf -e smtp_use_tls=yes
postconf -e mynetworks=0.0.0.0/0

You must change MX-hostname with your own, but that’s it.

Make sure only you have access to the server, by, for example, restricting all incoming connections to your web server, or at minimum port 25.

Configure Microsoft Exchange

This step requires you to have a paid Exchange plan from Microsoft. It might work with the free Outlook mailbox too, but we haven’t tested it.

First go to your Exchange control panel: https://admin.exchange.microsoft.com/

Find Connectors and click Add a connector

Add connector to Exchange

Choose Your organization’s email server

How would you connect to your Exchange

Name your integration, we choose the name of the application

Name your Exchange connection

Choose Verify by IP and enter the IP of your relay server.

IP of your Postfix relay server

Click Create. Now the Exchange setup is done.

Confirm Exchange settings and save

One Reply to “Setup Postfix Relay Server with Microsoft Exchange Email”

Leave a Reply

Your email address will not be published. Required fields are marked *