Monday, March 27, 2017

How to configure Postfix SMTP relay office365 on Centos 7.x

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:

CentOS 7 or Red Hat Enterprise Linux 7
Valid Office365 email credentials.

Installation:

Install Postfix using command below

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

Configuration changes:

Postfix main configuration file is main.cf, let's make the required change as follow:
[root@localhost ~]# 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
#smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Save and exit from the file.

The configuration of postfix SASL credentials:

[root@localhost ~]# vi /etc/postfix/sasl_passwd
Add a line below
[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@localhost ~]# postmap /etc/postfix/sasl_passwd
Now change permission for this file
[root@localhost ~]# chown root:postfix /etc/postfix/sasl_passwd
[root@localhost ~]# 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@localhost ~]# vi /etc/postfix/generic

Go the end of file and append following lines.
root@localhost.localdomain UserName@Domain.com

Save and exit from file.

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

Now Change alias root to your email address.

[root@localhost ~]# 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@localhost ~]# newaliases

Let's restart Postfix service.
[root@localhost ~]# systemctl restart postfix

Now try to send a test email using the command below:
FOR  Centos:

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

FOR Ubuntu:
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>"

You will get an email at recipeant@gmail.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@localhost ~]# usermod -c "Amar Singh" root

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

SMTP Relay Office365 has been configured successfully.


7 comments: