Sending emails via smtp relay from azure via office365



Not the snappiest of titles i know - and actually quite a dry post without really having any screenshots -but hey they can't all look great can they......

This is just a short summary of what i had to do to set up an smtp relay on a redhat linux machine running in the azure cloud - utilizing a mail account in office365. This would enable us to send emails directly from other machines in azure that could not easily directly integrate with office365 using 'modern' methods - at least not without some redevelopment effort - this setup process is another enabler in our short term goal of lift and shift type migrations versus long term goals of modernizing to use PaaS services.

So how did i go about this - here are the short series of steps required to get this working.

1) Install some extra software - postfix is the obvious mail tool - the other two are something that makes the bit with office365 work  can't say i know really exactly what they do (and i've not bothered to research it either....)

yum install postfix cyrus-sasl cyrus-sasl-md5

2) Update/add the following lines in /etc/postfix/main.cf the mail config file

relayhost = smtp.office365.com:587
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
sender_canonical_maps = regexp:/etc/postfix/canonical_maps_sender
inet_protocols = ipv4
smtp_sasl_auth_enable = yes
smtp_tls_security_level = may
smtp_sasl_security_options = noanonymous

The first line is obvious - it's just the smtp adress of office365 along with the port number, the next 2 lines reference additional files we'll create in a sec. The 4th lines just stops loads of annoying messages about ipv6. The last three are needed to make it work - don't ask me to expand on it any more than that.

3) Create the config file containing the credentials for office365

touch /etc/postfix/sasl_passwd
chown root: /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd

Once the file is there with the right permissions insert this content

smtp.office365.com yourusername@your.domain:yourpassword

Then get this content postmapped (not sure if this is necessary if you are restarting potfix- but i ran it anyway)

 postmap hash:/etc/postfix/sasl_passwd


4) Create the config file to remp the 'from' address to avoid the errors "550 5.7.60 SMTP; Client does not have permissions to send as this sender"


touch /etc/postfix/canonical_maps_sender

Then add the following entry - this remaps any from addresses postfix sees to the from address that is allowed by the account that is authenticating to office365.

/.*/ yourfromaddress@your.domain

then postmap this (again not 100% sure if this is needed or if a restart creates it anyway)

 postmap /etc/postfix/canonical_maps_sender

5) Then start the postfix daemon and set it to auto start on system startup

service postfix restart
chkconfig postfix on


And that's it - now if you configured everything right a simple test mail command like this should send you a mail just fine

echo test |mail -s this-is-test-subject anyemailaddress@any.domain

That will send an email from the office365 mailbox to whoever you want to send it to.

Pretty simple in the end to configure although i can tell i only scratched the surface of what postfix can do.......

All the clients have to do now to use the relay is point at the linux box on port 25 and away they go


0 comments:

Post a Comment