Empty Email Issue with MS365 Mail Relay from Python Application – Troubleshooting Guide

email-serverexchangemicrosoft-office-365

We've got a very weird issue going on here.

Take this example email (raw form, sanitized):

To: [email protected]
From: Thomas Ward via TestList <[email protected]>
Subject: Test Message
Date: Wed, 4 Aug 2021 19:44:49 -0400
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="------------EFA1B8DAB3C4E625DD16F705"
Content-Language: en-US
Sender: [email protected]
Reply-To: Thomas Ward <teward@NOPE>
CC: Thomas Ward <teward@NOPE>
Message-ID: <162812069430.22940.8470019517074758016@listserv-server>
List-Id: TestList <[email protected]>
List-Post: <mailto:[email protected]>

--------------EFA1B8DAB3C4E625DD16F705
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit

Test


--------------EFA1B8DAB3C4E625DD16F705
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Test<br>
    </p>
  </body>
</html>

--------------EFA1B8DAB3C4E625DD16F705--

This is being sent via straight SMTP to a Send Connector at MS365 configured to receive and relay mail from our on-prem mail server to MS365 for retransmission to the Internet.

Except… while the email retransmits to the destination external recipients… it actually only sends the To, From, Subject, etc. but completely ignores the actual content of the message. This was replicated with direct submission of a MIMEText() test email in Python as well.

The same email, however, when directly submitted to the external mail servers where I'm receiving this (where @NOPE would be my actual domain and server), transmits and relays fine, with all content. The same thing happens as well when we transmit via an on-prem Postfix relay which goes through a Sophos appliance, which then goes directly to the recipient server.

So, it seems that routing any mail via Exchange Online in this way with an on-prem system sending mail via MS365 outright fails to function. Regardless of the message submitted.

Has anyone else got any kind of on-prem mail system(s) or solutions here which need to route via MS365, and simply is unable to without Microsoft eating the message in its entirety?

The above email was sent to the Test List from external, routed through to an on-prem Exchange server (Exchange Hybrid setup due to on-prem listservs), modified for DMARC compliance, and then retransmitted to two external addresses as a new message, same issue at recipient ends. Also same issue for internal MS365 (in-organization) recipients.

Again, delivery works fine via on-prem Postfix stuff, but not when relayed via MS365 (which clearly ACCEPTED the emails and retransmitted everything EXCEPT the actual message).

Best Answer

OK, so it looks like I figured this out, but it's something I'll have to yell at Microsoft for.

The messages in my question are verbatim what is being sent to Postfix and what's being sent to Outlook SMTP connector. The difference is, however, that Postfix properly handles the message and then retransmits it somehow in a way that's different than the way Python's smtplib handles mail transmission. Messages and their contents get delivered properly when first handed to a Postfix server for processing before being sent out the door via the Send Connector at MS365. Which... is not what we were wanting because we didn't want to maintain any kind of actual mail server on prem, Postfix or otherwise, and just wanted to deliver directly to Microsoft 365 for handling rather than needing a 'middleman hop' in a mail server and MTA before receiving messages from endpoint clients and connectors.

It's also odd that Microsoft 365 is incapable of properly handing messages handed directly to it, unless Microsoft has some weird format it wants to handle that isn't RFC compliant... but that's a different fight.