Gordan Jandreoski

Gordan Jandreoski

var blog = { "dev", "cloud", "tech", "geek", null};

Newsletter Subscribe


How to configure postfix relay to Office365 on Ubuntu 14.04

Gordan JandreoskiGordan Jandreoski

Linux tips and tricks

Although I primary work as Full Stack .Net Developer; I have knowledge in lots of areas including Unix, mobile development and PHP development. So I wasn't surprised when my boss asked me to help the PHP team with some Wordpress custom development.

One problem my team had was sending email thru Wordpress. Since PHP executes synchronous when we needed to send an email the request last very long. I tried implementing a custom cron function that will call the wp_mail(). That was working just fine, but other third party plugins were still using the native wp_mail() function.

I decide installing and configuring local postfix mail server on Ubuntu 14.04 to reduce the time needed for sending an email in Wordpress, and relay all emails to Office365. After several tries I successfully installed and configured postfix to relay all email to Office365.

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

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/psotfix/main.cf

sudo nano /etc/psotfix/main.cf

For the relayhost add smtp.office365.com and port 587

relayhost = [smtp.office365.com]: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:

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.

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:

sudo nano /etc/postfix/sasl_passwd

and add this line to the file

[smtp.office365.com]:587 [email protected]:office365password

Change the [email protected] 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.

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).

sudo nano /etc/postfix/generic

and add this two lines

[email protected] [email protected]
@localdomain [email protected]

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

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.

sudo service postfix restart
echo "This is a test body" | mail -s "Relay Test Email" [email protected] -a "FROM:[email protected]"

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 [email protected] to other user in the same @domain.com domain and in the main.cf file for the mydestination setting you have added domain.com then the email will not be sent because postfix thinks this is a local relay. Try removing domain.com from the mydestination setting.