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).
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
sudo nano /etc/postfix/main.cf
smtp.office365.com and port
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 = 127.0.0.0/8 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 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
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
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
root@localdomain email@example.com @localdomain firstname.lastname@example.org
email@example.com with your Office365 account used previously. Now lets set correct file permissions and hash the file for use in
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" user@publicdomain -a "FROM:firstname.lastname@example.org"
If you did not receive the test email then check the log files at
If you you are trying to send an email from
email@example.com 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