Tuesday, October 3, 2017

How to configure Postfix SMTP relay office365 on Ubuntu 16.04

Postfix:

Postfix is a flexible mail server that is available on most Linux distribution. Though a full featured mail server, Postfix can also be used as a simple relay host to another mail server, or smart host. This tutorial will describe how to configure Postfix as a relay through Office365.



Requirement:

Ubuntu 16.04 box
Valid Office365 email credentials.

Installation:

Install Postfix using command below

root@U16:~# apt install postfix
during installation wizard it will ask for mail type configuration then select Internet site

Again click on OK when ask for hostname.
Installation of postfix has done.

Configuration changes:

Postfix main configuration file is main.cf, let's make the required change as follow:
root@U16:~# vi /etc/postfix/main.cf
 Append the following line
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
Save and exit from the file.

postfix SASL credentials configuration:

root@U16:~# vi /etc/postfix/sasl_passwd
Append following info.
[smtp.office365.com]:587 user@domainname:password
Save and exit from file

A Postfix lookup table must now be generated from the sasl_passwd text file by running the following command.
root@U16:~# postmap /etc/postfix/sasl_passwd
Now change permission for this file
root@U16:~# chown root:postfix /etc/postfix/sasl_passwd
root@U16:~# chmod 640 /etc/postfix/sasl_passwd
Next, we need to configure generic file in order to be able to send emails as a valid user (this is required for Office365).
root@U16:~# vi /etc/postfix/generic
Save and exit from file.

Next let's correct the file permission.
root@U16:~# chown root:root /etc/postfix/generic
root@U16:~# chmod 0600 /etc/postfix/generic
root@U16:~# postmap /etc/postfix/generic


Now Change alias root to your email address.
root@U16:~ vi /etc/aliases

Make the changes like below:
mailer-daemon:  postmaster
postmaster:     root
root:           UserName@Domain.com
Save and exit from the file.
Run command newaliases to take effect.
root@U16:~ newaliases

Let's restart Postfix service.
root@U16:~# systemctl restart postfix.service

send a test email using the command below:
echo "This is the body of the email" | mail -s "This is the subject(E-Mail from SMTP Relay) line" recipeat@gmail.com -a "FROM:Amar Singh<asingh@domain.com>"

Change root display name to Actual user name:

Bydefault when we send an email display name will be root let's modify root user's description by using the command below:

root@U16:~# usermod -c "Amar Singh" root

SMTP Relay Office365 has been configured successfully.

5 comments:

  1. what is the content of the genric file ?

    ReplyDelete
  2. Nice post, this was helpful to me setting up a Nagios VM inside of Azure to use O365 for alerts on Azure VMs, been a while since I set up the Postfix on our internal Nagios server, so this made it quite easy for me.

    ReplyDelete
  3. vi /etc/postfix/generic, What is the configuration of generic file.

    ReplyDelete