Our requirement is to set up an SMTP relay on a Linux machine that will send notification emails to one or more customers through the Office365 connector. This Linux machine as an Application server will notify any application errors or any job failures. SMTP relay is mostly used for sending outgoing mail.

Pre-requisite:

  1. You have taken the office 365 license
  2. The public domain is setup and ready to use
  3. Mail server records have been added to the DNS hosting server
  4. SMTP servers need public IP or else need internet access via a proxy
  5. Outgoing traffic on port 25 should be opened
  6. email account to test email functionality

High-level steps:

A. Setup connector
B. SMTP server setup in Linux
C. Test outgoing mail via SMTP

A. Setup Connector

1. First thing I will take static public IP for the SMTP relay server.

2. Login into the domain registrar website where you have added your Mail related records

3. Go to the domain setting of your domain

4. Modify the value of spf record as shown below. I have added the static public IP of the SMTP server. This addition for your outgoing mail does not go into the spam mailbox of customers.

v=spf1 ipv4:1.186.10.10 include:spf.protection.outlook.com -all

Imp Note: You can add the public IP of that server or Proxy or firewall IP in the value of the TXT record through which it is going to the Internet

5. Login into the M365 admin center and expand all options, you will find the exchange over there, click on it. A new window of the Exchange admin center will open.

6. Go to Mail Flow -> connector

7. Create a new connector.

o365 connector

8. Provide metadata

Connector Details

9. Authentication to send mail.

Allowed specific IP to send a mail

10. Review the summary page and create the connector.

B. SMTP server setup in Linux

1. Spin-up Linux machine with centos latest version

2. Run the below commands to install the postfix tool

#dnf -y install postfix             -->  tool installation
#systemctl start postfix            --> start postfix service
#systemctl enable postfix           --> enable postfix service
#systemctl status postfix           --> check service status

3. Install the telnet tool with the below command. This is an additional tool for troubleshooting
#yum install telnet

4. Open configuration file on the path /etc/postfix/main.cf.

5. Uncomment myhostname parameter and set the value

Hostname of SMTP Server

6. Uncomment myorigin parameter that will take value from the above parameter

Set Origin

7. Check mydestination parameter, uncomment the first line

Set Destination

8. Set local loopback IP range for mynetwork

Set Network

9. Set exchange server name (MX record value) as smart host with the port number. MX record value is available with office365 admin.

Set Relayhost

10. Keep the default setting as it is. There is additional security setting in the config, where you can set up a user and password to send a mail.

11. Test configuration and restart the service
#Check postfix –> test configuration
#systemctl restart postfix –> restart postfix service

C. Test outgoing mail via SMTP

1. Send mail through the command line.

#echo “Subject: Mail Delivery Test” > /tmp/mail.txt
#echo “This is the example e-mail body” >> /tmp/mail.txt
#sendmail -v mailuser1@spcloud.cf < /tmp/mail.txt

2. Recipient will receive a mail in the inbox.

User Mailbox

This is how you can set up an SMTP relay on any Linux machine in a simple way.