How to Configure Postfix Email Relay via Office 365

Tutorial on how to configure Postfix Email Relay via Office 365

Install Postfix

dnf install postfix

Package cyrus-sasl-plain contains the Cyrus SASL plugins which support PLAIN and LOGIN authentication.

dnf install cyrus-sasl-plain

Create a new file /etc/postfix/sasl_passwd to store the credential of [email protected]

# Enter Office 365 Credential - [smtp.office365.com]:587 LOGIN_EMAIL:PASSWORD
vi /etc/postfix/sasl_passwd
      [smtp.office365.com]:587 [email protected]:XXXXXXXXXXX

# change permissions to file so only root would be able to access it
chmod 0600 /etc/postfix/sasl_passwd

# Create postfix database
postmap /etc/postfix/sasl_passwd

Login to Microsoft 365 with PowerShell to enable SMTP AUTH for individual user

The SMTP AUTH protocol is used for client SMTP email submission (typically on TCP port 587). SMTP AUTH doesn’t support modern authentication (Modern Auth), and only uses basic authentication, so all you need to send email messages is a username and password. This makes SMTP AUTH a popular choice for attackers to send spam or phishing messages using compromised credentials.

$User = "Notification"
Get-CASMailbox $User | Set-CASMailbox -SmtpClientAuthenticationDisabled $false 

Backup the original /etc/postfix/main.cf and create a new main.cf with the following

[root@centos ~]# mv /etc/postfix/main.cf /etc/postfix/main.cf.bak
[root@centos ~]# nano /etc/postfix/main.cf
compatibility_level = 2
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
home_mailbox = Maildir/
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix/samples
readme_directory = /usr/share/doc/postfix/README_FILES
smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtpd_tls_security_level = may
smtp_tls_CApath = /etc/pki/tls/certs
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
smtp_tls_security_level = may
meta_directory = /etc/postfix
shlib_directory = /usr/lib64/postfix
# Replace with your own 
myhostname = centos.aventis365.info
mydomain = aventis365.info
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mynetworks = 10.0.0.0/24
relayhost = [smtp.office365.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes

Restart Postfix service

systemctl restart postfix

Create a new user called notification

You can only send from one email address unless your device can store login credentials for multiple Microsoft 365 or Office 365 mailboxes. Microsoft 365 or Office 365 imposes a limit of 30 messages sent per minute, and a limit of 10,000 recipients per day. – [SMTP AUTH client submission](How to set up a multifunction device or application to send email using Microsoft 365 or Office 365 | Microsoft Docs)

useradd notification
passwd notification

Login with notification user to verify whether email is delivery successfully via M365

[notification@centos ~]$ echo "Subject: Mail Delivery Test" > /home/notification/mail.txt
[notification@centos ~]$ echo "This is the example e-mail body" >> /home/notification/mail.txt

[notification@centos ~]$ sendmail -v [email protected] < /home/notification/mail.txt
Mail Delivery Status Report will be mailed to <notification>.

Verify the Email is delivered successfully

[root@centos ~]# journalctl -u postfix
-- Logs begin at Tue 2021-04-20 14:05:02 +08, end at Tue 2021-04-20 18:06:28 +08. --
Apr 20 17:52:28 centos.aventis365.info systemd[1]: Starting Postfix Mail Transport Agent...
Apr 20 17:52:29 centos.aventis365.info postfix/master[2247]: daemon started -- version 3.3.1, configuration /etc/postfix
Apr 20 17:52:29 centos.aventis365.info systemd[1]: Started Postfix Mail Transport Agent.
Apr 20 17:56:45 centos.aventis365.info systemd[1]: Stopping Postfix Mail Transport Agent...
Apr 20 17:56:45 centos.aventis365.info systemd[1]: postfix.service: Succeeded.
Apr 20 17:56:45 centos.aventis365.info systemd[1]: Stopped Postfix Mail Transport Agent.
Apr 20 17:56:45 centos.aventis365.info systemd[1]: Starting Postfix Mail Transport Agent...
Apr 20 17:56:46 centos.aventis365.info postfix/master[2340]: daemon started -- version 3.3.1, configuration /etc/postfix
Apr 20 17:56:46 centos.aventis365.info systemd[1]: Started Postfix Mail Transport Agent.
Apr 20 18:06:23 centos.aventis365.info postfix/pickup[2341]: 69ACF188E72B: uid=1001 from=<notification>
Apr 20 18:06:23 centos.aventis365.info postfix/cleanup[2403]: 69ACF188E72B: message-id=<[email protected]>
Apr 20 18:06:23 centos.aventis365.info postfix/qmgr[2342]: 69ACF188E72B: from=<[email protected]>, size=269, nrcpt=1 (queue active)
Apr 20 18:06:26 centos.aventis365.info postfix/smtp[2405]: 69ACF188E72B: to=<[email protected]>, relay=smtp.office365.com[40.100.28.194]:587, delay=2.9, delays=0.02/0.03/2/0.78, dsn=2.0>
Apr 20 18:06:26 centos.aventis365.info postfix/cleanup[2403]: 505241893D09: message-id=<[email protected]>
Apr 20 18:06:26 centos.aventis365.info postfix/qmgr[2342]: 505241893D09: from=<>, size=2267, nrcpt=1 (queue active)
Apr 20 18:06:26 centos.aventis365.info postfix/bounce[2407]: 69ACF188E72B: sender delivery status notification: 505241893D09
Apr 20 18:06:26 centos.aventis365.info postfix/qmgr[2342]: 69ACF188E72B: removed
Apr 20 18:06:28 centos.aventis365.info postfix/smtp[2405]: 505241893D09: to=<[email protected]>, relay=smtp.office365.com[52.98.40.34]:587, delay=2.3, delays=0.01/0/1.7/0.56>
Apr 20 18:06:28 centos.aventis365.info postfix/qmgr[2342]: 505241893D09: removed

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top