Steps to getting FreePBX to work with Office 365 email
Using the Commercial System Admin module makes this a bit easier. It would still be possible to do this without the Commercial System Admin module, but you will need to configure many more options manually through editing the files of course. See "Setup Postfix Manually" (below) in the references section.
Many of the Asterisk, FreePBX, Linux gurus out there don't yet fully understand, is that Office 365 is more paranoid than most SMTP systems. It requires a few things before an email can be relayed, one of them being from a known account, and the other one being that it has to be from a known good IP. These type of setups will become standard fare for new email systems, and will be accepted best practice going forward.
Requirements for this method:
1. You are using a fixed IP address
2. You don't need IPv6 on your PBX (as Office 365 does not support it, yet)
3. A known good Office 365 account and password
4. Optional, but highly recommended: Commercial System Admin module
NOTE: There is another method that you can use to setup emails for FreePBX and Office 365. I had this working nicely as well, and that is to use another SMTP relay on your local network. I did that with my IIS server SMTP relay, and it worked great. I would prefer using that method actually, however, not all my clients have an on-prem setup like that, and I wanted to ensure that I could get this to work with their Office 365 setup as is. If you want more information on how to setup IIS with SMTP relay, see reference #4 at the end of this post.
Steps
[1] Tell Office 365 you are "coming to dinner". In that sense you will need to setup an SPF text record for your domain that informs Office 365 that you are a "good boy", and that your public facing IP address can be trusted. Since your SPF record already exists for your domain for Office 365, you will not so much need to add it, but modify it so that it include the external IP of your PBX to the following:
v=spf1 ip4:Static IP Address include:spf.protection.outlook.com ~all <-- Where it says ip4: you put in your pbx address
Note, that if you already have an ip4: in there with an IP, you can add another.
[2] Modify your /etc/postfix/generic by adding these lines, replacing RealAccountInOffice365@YourRealDomain.com with a real email account in your Office 365 system.
root RealAccountInOffice365@YourRealDomain.com
root@localhost RealAccountInOffice365@YourRealDomain.com
root@localhost.localdomain RealAccountInOffice365@YourRealDomain.com
root@freepbx RealAccountInOffice365@YourRealDomain.com
root@freepbx.localdomain RealAccountInOffice365@YourRealDomain.com
asterisk RealAccountInOffice365@YourRealDomain.com
asterisk@localhost RealAccountInOffice365@YourRealDomain.com
asterisk@localhost.localdomain RealAccountInOffice365@YourRealDomain.com
asterisk@freepbx RealAccountInOffice365@YourRealDomain.com
asterisk@freepbx.localdomain RealAccountInOffice365@YourRealDomain.com
vm@asterisk RealAccountInOffice365@YourRealDomain.com
@freepbx.localdomain @YourRealDomain.com
[3] Add the following to the end of /etc/postfix/main.cf
smtp_generic_maps = hash:/etc/postfix/generic
and
inet_protocols = ipv4
Reload postfix by running these commands:
postmap /etc/postfix/generic
service postfix restart
NOTE/OPTIONAL: I also took the liberty to disable IPv6 at the Centos OS level as well. I do not need IPv6, nor is it supported end to end everywhere, so I don't want it to ever be an issue until I know IPv6 is supported everywhere. If you want to disable IPv6 at the OS level you can do so by going to /etc/sysctl.conf and adding the following line: net.ipv6.conf.all.disable_ipv6 = 1 save the file, then reload the system ip configuration by issuing this command: sysctl -p
[4] Using the Commercial System Admin module, go to Email Setup. Use these settings:
Provider: Other <-- Don't use Office 365, as you want explicit control of the settings here
SMTP Server: Use External SMTP Server
My Hostname: whatever.YourRealDomain.com
My Origin: whatever.YourRealDomain.com
My Domain: whatever.YourRealDomain.com
SMTP Server: smtp.office365.com:587
Use Auth: Use Authentication
Username: RealEmailAddressOfOffice365Account@YourRealDomain.com
Password: TheRealPassword
[5] Click on the Submit button
[6] Click on Debug, and then use a real email address to sent it to. The output of the debug should show you want is right/wrong and should help if you still have issues with the setup.
References:
1. Setup Postfix Manually - https://wiki.freepbx.org/display/PPS/Setup+Postfix+Manually
2. How to set up a multifunction device or application to send email using Office 365 - See section 3, click on "Settings for Office 365 SMTP relay" https://support.office.com/en-us/article/How-to-set-up-a-multifunction-device-or-application-to-send-email-using-Office-365-69f58e99-c550-4274-ad18-c805d654b4c4
3. Turning off IPv6 on CentOS - http://www.networkinghowtos.com/howto/disable-ipv6-on-centos/
4. How to configure IIS for relay with Office 365 - https://support.office.com/en-us/article/How-to-configure-IIS-for-relay-with-Office-365-eb57abd2-3859-4e79-b721-2ed1f0f579c9