How to configure postfix relay to Office365 on Ubuntu

First we need to install postfix server and mailutils (simple mail commands that will help testing our configuration).

1 2 sudo apt-get update sudo apt-get install postfix mailutils

On the configuration page chose “Internet Site” and continue with the default setting in the next step. Since the whole purpose of our mail server is to just relay all messages to Office365 it is not necessary to configure external domain.

Once installation completes we need to configure postfix; open /etc/postfix/

1 sudo nano /etc/postfix/

For the relayhost add and port 587

1 relayhost = []:587

Since we will be using this mail server locally we want to protect it by saying that we only accept connections from localhost. So modify this two setting:

1 2 mynetworks = inet_interfaces = loopback-only

Normally you want to use secure connection to Office365 so it is necessary to configure postfix to use TLS. Add this code at the end of the config file and save the file.

1 2 3 4 5 6 7 8 9 smtp_use_tls = yes smtp_always_send_ehlo = yes smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_sasl_tls_security_options = noanonymous smtp_tls_security_level = encrypt smtp_generic_maps = hash:/etc/postfix/generic smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Next we need to configure the sasl_passwd and generic files.

The sasl_passwd holds information for the Office365 account used to relay the emails. Create the file by typing:

1 sudo nano /etc/postfix/sasl_passwd

and add this line to the file

1 []:587 username@yourdomain:office365password

Change the username@yourdomain with your account and office365password with the account password and save the file.

Next we need to set correct file permissions and hash the file for use in postfix.

1 2 3 sudo chown root:root /etc/postfix/sasl_passwd sudo chmod 0600 /etc/postfix/sasl_passwd sudo postmap /etc/postfix/sasl_passwd

Next we need to configure generic file in order to be able to send emails as a valid user (this is required for Office365).

1 sudo nano /etc/postfix/generic

and add this two lines

1 2 root@localdomain @localdomain

substitute with your Office365 account used previously. Now lets set correct file permissions and hash the file for use in postfix.

1 2 3 sudo chown root:root /etc/postfix/generic sudo chmod 0600 /etc/postfix/generic sudo postmap /etc/postfix/generic

Our configuration is done. Lets restart postfix and test.

1 2 sudo service postfix restart echo "This is a test body" | mail -s "Relay Test Email" user@publicdomain -a ""

If you did not receive the test email then check the log files at /var/log/mail.log

Common problems

If you you are trying to send an email from to other user in the same domain and in the file for the mydestination setting you have added then the email will not be sent because postfix thinks this is a local relay. Try removing from the mydestination setting.

Copyright © Asia Pacific University. All Rights Reserved.