SMTP – Using Office 365 email account with Postfix relay on Ubuntu

on

In this case we wanted to use an email account from Office 365 to send emails via postfix, to use as the native mail function for WordPress

This is based on Ubuntu 20.04 LTS

sudo apt-get update
sudo apt-get install postfix mailutils

In the menu, just use the default entry (Internet Site)

Configuring postfix

sudo nano /etc/postfix/main.cf

Here we need to change the following

relayhost = [smtp.office365.com]:587
mynetworks = 127.0.0.0/8
inet_interfaces = ipv4

Add the following at the end of 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

Create the file that will hold the login info

sudo nano /etc/postfix/sasl_passwd
[smtp.office365.com]:587 user@yourdomain:password

Set file permissions for the password

sudo chown root:root /etc/postfix/sasl_passwd
sudo chmod 0600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Configuring generic to define the user

sudo nano /etc/postfix/generic

Add these two lines replacing your email user

root@localdomain [email protected]
@localdomain [email protected]

Set file permissions for generic

sudo chown root:root /etc/postfix/generic
sudo chmod 0600 /etc/postfix/generic
sudo postmap /etc/postfix/generic

Restart Postfix

sudo service postfix restart

In Office 365, change SMTP Authentication for the user via GUI or Powershell

Or via Powershell:

Set-CASMailbox -Identity [email protected] -SmtpClientAuthenticationDisabled $false

Finally, to test the email flow:

echo "Test body" | mail -s "Relay Test Email" [email protected] -a "FROM:[email protected]"

 

Leave a Reply

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