Categories
Knowledge Base

Setting up Postfix to use Office 365 mail

FreePBX uses centos 7 and postfix fom its mail delivery, normally this is fine unless the customer is using Office 365 mail then there can be delivery issues.

Firstly you will need to set up a user in Office 365 for the system.

Postfix’s main configuration file is main.cf and that is where we make the required change as follow:

[root@localhost ~]# vi /etc/postfix/main.cf

Append the following lines

masquerade_domains = domainname
myhostname = USERNAME.domainname
mydomain = USERNAME.domainname
myorigin = USERNAME@domainname
relayhost = [smtp.office365.com]:587

mynetworks = 127.0.0.0/8
inet_interfaces = loopback-only
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

Save and exit from the file.
Next we need to edit the configuration for the postfix SASL credentials:

[root@localhost ~]# vi /etc/postfix/sasl_passwd

Add a line below

[smtp.office365.com]:587 user@domainname:password

Replacing user@domainname:password with your sender account details

Save and exit from file

A Postfix lookup table must now be generated from the sasl_passwd text file by running the following command.

[root@localhost ~]# postmap /etc/postfix/sasl_passwd

Now change permission for this file

[root@localhost ~]# chown root:postfix /etc/postfix/sasl_passwd

[root@localhost ~]# chmod 640 /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).

[root@localhost ~]# vi /etc/postfix/generic

Go the end of file and append following lines.

root@localhost.localdomain UserName@Domain.com

Again replacing localhost.localdomain and UserName@Domain.com with your service hostname and the email user are using

Save and exit from file.

Next let’s correct the file permission.

[root@localhost ~]# chown root:root /etc/postfix/generic

[root@localhost ~]# chmod 0600 /etc/postfix/generic

[root@localhost ~]# postmap /etc/postfix/generic

Now restart Postfix service.

[root@localhost ~]# systemctl restart postfix

Now try to send a test email using the command below:
FOR Centos:

echo "This is the body of the email"| mail -r"Sender-Display-Name<sender@domain.com>" -s "This is the subject(E-Mail from SMTP Relay) line" recipeat@gmail.com

In FreePBX under Voicemail admin you must change the senders address to match your account as well as the sender for notifications such as backups etc. otherwise you can get errors and mail wont be delivered.