SMTP relay using office365 for FreePBX or rhel based distros

FreePBX use a paid module called System Admin PRO for easily config smtp server settings, but if you cant pay here is a example of manual config using a office365 email account

This process can be useful for sending EMAILS using office365 account through Centos 7 server, or based like SANGOMA Linux used by FreePBX

Install packages

[root@localhost ~]# yum install postfix cyrus-sasl-plain mailx

Edit config file of postfix (vim or prefered text editor)

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

Append the configuration

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

Credentials for sending account

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

Add a line and EDIT EMAIL ACCOUNT AND PASSWORD

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

Save and exit from text editor

Exec postmap for create lookup table:

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

Change permissions of file

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

Configuration of generic file in order to be able to send emails as a valid user (required for Office365)

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

Append these line and EDIT USER AND DOMAIN:

root@localhost.localdomain user@domain.com

Save the file and exit from text editor

Change file permissions for security reasons

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

Change alias root to email address

[root@localhost ~]# vim /etc/aliases

Make changes like (EDIT USER AND DOMAIN)

mailer-daemon: postmaster
postmaster: root
root: user@domain.com

Save and exit from text editor

Rebuild the data base for the mail aliases file

[root@localhost ~]# newaliases

Restart postfix service

[root@localhost ~]# systemctl restart postfix

Test config sending a email (EDIT SENDER EMAIL AND RECIPIENT)

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

You can debug the process monitoring /var/log/maillog file

Oct 31 15:01:24 freepbx postfix/smtp[20049]: 0FD4EA2329F: to=<jose@marcano.co>, relay=smtp.office365.com[40.97.29.50]:587, delay=3.1, delays=0.14/0.01/2.2/0.83, dsn=2.0.0, status=sent (250 2.0.0 OK <5bda0a10.gdprwfIjkuhmbHGS%telefoniaip@araujoibarra.com> [Hostname=BN3PR19MB0595.namprd19.prod.outlook.com])
Oct 31 15:01:24 freepbx postfix/qmgr[8181]: 0FD4EA2329F: removed

When we send an email display name will be root let’s modify root user’s description
Edit “My Name”

[root@localhost ~]# usermod -c "My Name" root

Now sender name has been changed to the actual user name.