Configuring Postfix Relay to Office365 on Proxmox

CPBPILOT

New Member
Jun 30, 2023
12
5
3


Thought I would share this as I have been fighting it for a few days. I was able to finally get postfix setup with office365 so that sendmail would work. This is mostly a copy/paste of this site so I don't claim to be the originator but I had to insure saslauthd was installed and running before it worked. That's the part that was not in the original write-up.​



Step 1: Install Required Packages

First, update your package list and install the necessary mail utilities:

Code:
apt-get update
apt-get install postfix mailutils sasl2-bin libsasl2-modules

Step 2: Configure Postfix​

Edit the main Postfix configuration file:

Code:
nano /etc/postfix/main.cf
Modify or add the following lines to configure the relay:

Code:
relayhost = [smtp.office365.com]:587
mynetworks = 127.0.0.0/8
inet_interfaces = loopback-only
inet_protocols = ipv4

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 the file CTRL+X, then Y, then Enter.

Step 3: Configure Authentication (sasl_passwd)​

Create the authentication file:

Code:
nano /etc/postfix/sasl_passwd
Add the following line, replacing username@yourdomain.com and password with your Office365 credentials:

Code:
[smtp.office365.com]:587 username@yourdomain.com:password
Set the correct permissions and hash the file:

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

Step 4: Configure Generic Mapping​

Create the generic file:

Code:
nano /etc/postfix/generic
Add the following lines:

Code:
root@localdomain username@yourdomain.com
@localdomain username@yourdomain.com

Set the correct permissions and hash the file:

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

Step 5: Ensure saslauthd is Installed and Running​

Check if saslauthd is installed and running:

Code:
systemctl status saslauthd
If it is not active, enable and start it:

Code:
systemctl start saslauthd

systemctl enable saslauthd

Step 6: Test the Configuration​

Restart Postfix:

Code:
systemctl restart postfix
Send a test email:

Code:
echo "This is a test email" | mail -s "Relay Test Email" recipient@example.com -a "From: username@yourdomain.com"

Step 7: Troubleshooting​

If you do not receive the test email, check the logs:

tail -f /var/log/mail.log
If sending to another user in the same domain yourdomain.com fails, check the mydestination setting in main.cf and remove yourdomain.com if present.