Skip to content

fix: resolve Outlook 365 sender address and convert HTML-only message bodies - #2245

Draft
gaurav0107 wants to merge 2 commits into
microsoft:mainfrom
gaurav0107:fix/1188-outlook-msg-sender-and-html-body
Draft

fix: resolve Outlook 365 sender address and convert HTML-only message bodies#2245
gaurav0107 wants to merge 2 commits into
microsoft:mainfrom
gaurav0107:fix/1188-outlook-msg-sender-and-html-body

Conversation

@gaurav0107

Copy link
Copy Markdown

Summary

.msg files saved from Outlook 365 / Exchange lose two things on conversion. Both are reproduced against main below.

1. From is an Exchange DN, not an address. The converter read the sender only from PR_SENDER_EMAIL_ADDRESS (0x0C1F). That property holds an email address only when PR_SENDER_ADDRTYPE (0x0C1E) is SMTP; anything routed through Exchange sets the address type to EX and stores a Distinguished Name there instead, so the header rendered as:

**From:** /O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=ab12cd34

The sender is now resolved in MAPI-correct order — PR_SENDER_SMTP_ADDRESS (0x5D01), then PR_SENT_REPRESENTING_SMTP_ADDRESS (0x5D02), then the legacy properties but only when their address type is not the DN form. If a file genuinely carries nothing else, the DN is still emitted, so no message loses its From header.

2. HTML-only messages render an empty ## Content. The body was read only from PR_BODY (0x1000), which Outlook on the web leaves empty for HTML-only mail — the entire message body was silently dropped. It now falls back to PR_HTML (0x1013) and converts it with the HtmlConverter this repo already ships, the same way PptxConverter / XlsxConverter / EpubConverter delegate. Both the binary (0102) and string (001F) variants of the property are handled, and the byte stream's charset is detected with charset_normalizer — already a core dependency, used the same way in _csv_converter.py and _plain_text_converter.py — rather than assuming UTF-8.

Binary streams are read through a new _read_stream helper rather than _get_stream_data, because that function decodes UTF-16-LE first and arbitrary even-length bytes almost never fail that decode: PR_HTML would come back as mojibake instead of raising. _get_stream_data keeps its exact previous signature and decoding behavior.

No new dependency, and plain-text messages take the identical path they took before — the HTML fallback is reached only when PR_BODY is absent or empty.

Before / after on the message shape described in the issue:

**From:** /O=EXCHANGELABS/OU=.../CN=RECIPIENTS/CN=ab12cd34   ->  **From:** sender@example.com
## Content                                                    ->  ## Content
(empty)                                                              Hello **world**

Testing

New packages/markitdown/tests/test_outlook_msg.py, 11 cases: SMTP-over-DN preference, the sent representing variants, the plain-SMTP case (unchanged behavior), the last-resort DN fallback, HTML-only body conversion, the string-variant property, non-UTF-8 (cp1252) HTML bytes, absent HTML, plain-text-wins-over-HTML, and an end-to-end convert() of the reported message shape.

A .msg file is an OLE2 compound file and nothing in the dependency set can write that container, so a synthetic binary fixture is not possible here the way test_svg_no_fallback.pptx was. The tests instead serve the MAPI streams through a stand-in for olefile.OleFileIO, matching the stub-the-collaborator style already used in tests/test_docintel_html.py and tests/test_cu_converter.py. Happy to swap in a real fixture if a maintainer can share a sanitised Exchange .msg.

pytest tests/test_outlook_msg.py            11 passed
pytest tests/ (full suite)                  347 passed, 3 skipped
black --check .                             73 files unchanged
mypy --ignore-missing-imports               no issues

The existing test_outlook_msg.msg vector is unaffected — that fixture has PR_SENDER_ADDRTYPE == "SMTP", so **From:** test.sender@example.com still holds.

The only full-suite failure, test_module_misc.py::test_speech_transcription, fails identically on a clean checkout of main: the online recogniser returns 1 2 3 4 for a clip the test expects to end in 5. Unrelated to this change.

Fixes #1188

OutlookMsgConverter read the sender only from PR_SENDER_EMAIL_ADDRESS
(0x0C1F). That property holds an email address only when
PR_SENDER_ADDRTYPE (0x0C1E) is "SMTP"; messages sent through Exchange set
it to "EX" and store a Distinguished Name there instead, so From rendered
as "/O=EXCHANGELABS/OU=.../CN=RECIPIENTS/CN=...". Resolve the sender from
PR_SENDER_SMTP_ADDRESS / PR_SENT_REPRESENTING_SMTP_ADDRESS first, use the
legacy properties only when their address type is not the DN form, and
keep the DN as a last resort so the header is never dropped.

The body was likewise read only from PR_BODY (0x1000), which HTML-only
messages leave empty, so the whole message silently vanished from
"## Content". Fall back to PR_HTML (0x1013) and convert it with the
existing HtmlConverter, handling both the binary and string variants of
the property and detecting the byte stream's charset rather than assuming
UTF-8. Binary streams are read through a new _read_stream helper because
_get_stream_data decodes UTF-16 first, which rarely fails on arbitrary
bytes and would return mojibake instead of raising.

Fixes microsoft#1188
@microsoft-github-policy-service

Copy link
Copy Markdown

@gaurav0107 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"
Contributor License Agreement

Contribution License Agreement

This Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
and conveys certain license rights to Microsoft Corporation and its affiliates (“Microsoft”) for Your
contributions to Microsoft open source projects. This Agreement is effective as of the latest signature
date below.

  1. Definitions.
    “Code” means the computer software code, whether in human-readable or machine-executable form,
    that is delivered by You to Microsoft under this Agreement.
    “Project” means any of the projects owned or managed by Microsoft and offered under a license
    approved by the Open Source Initiative (www.opensource.org).
    “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any
    Project, including but not limited to communication on electronic mailing lists, source code control
    systems, and issue tracking systems that are managed by, or on behalf of, the Project for the purpose of
    discussing and improving that Project, but excluding communication that is conspicuously marked or
    otherwise designated in writing by You as “Not a Submission.”
    “Submission” means the Code and any other copyrightable material Submitted by You, including any
    associated comments and documentation.
  2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any
    Project. This Agreement covers any and all Submissions that You, now or in the future (except as
    described in Section 4 below), Submit to any Project.
  3. Originality of Work. You represent that each of Your Submissions is entirely Your original work.
    Should You wish to Submit materials that are not Your original work, You may Submit them separately
    to the Project if You (a) retain all copyright and license information that was in the materials as You
    received them, (b) in the description accompanying Your Submission, include the phrase “Submission
    containing materials of a third party:” followed by the names of the third party and any licenses or other
    restrictions of which You are aware, and (c) follow any other instructions in the Project’s written
    guidelines concerning Submissions.
  4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else
    for whom You are acting in making Your Submission, e.g. as a contractor, vendor, or agent. If Your
    Submission is made in the course of Your work for an employer or Your employer has intellectual
    property rights in Your Submission by contract or applicable law, You must secure permission from Your
    employer to make the Submission before signing this Agreement. In that case, the term “You” in this
    Agreement will refer to You and the employer collectively. If You change employers in the future and
    desire to Submit additional Submissions for the new employer, then You agree to sign a new Agreement
    and secure permission from the new employer before Submitting those Submissions.
  5. Licenses.
  • Copyright License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license in the
    Submission to reproduce, prepare derivative works of, publicly display, publicly perform, and distribute
    the Submission and such derivative works, and to sublicense any or all of the foregoing rights to third
    parties.
  • Patent License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under
    Your patent claims that are necessarily infringed by the Submission or the combination of the
    Submission with the Project to which it was Submitted to make, have made, use, offer to sell, sell and
    import or otherwise dispose of the Submission alone or with the Project.
  • Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement.
    No additional licenses or rights whatsoever (including, without limitation, any implied licenses) are
    granted by implication, exhaustion, estoppel or otherwise.
  1. Representations and Warranties. You represent that You are legally entitled to grant the above
    licenses. You represent that each of Your Submissions is entirely Your original work (except as You may
    have disclosed under Section 3). You represent that You have secured permission from Your employer to
    make the Submission in cases where Your Submission is made in the course of Your work for Your
    employer or Your employer has intellectual property rights in Your Submission by contract or applicable
    law. If You are signing this Agreement on behalf of Your employer, You represent and warrant that You
    have the necessary authority to bind the listed employer to the obligations contained in this Agreement.
    You are not expected to provide support for Your Submission, unless You choose to do so. UNLESS
    REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, AND EXCEPT FOR THE WARRANTIES
    EXPRESSLY STATED IN SECTIONS 3, 4, AND 6, THE SUBMISSION PROVIDED UNDER THIS AGREEMENT IS
    PROVIDED WITHOUT WARRANTY OF ANY KIND, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY OF
    NONINFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  2. Notice to Microsoft. You agree to notify Microsoft in writing of any facts or circumstances of which
    You later become aware that would make Your representations in this Agreement inaccurate in any
    respect.
  3. Information about Submissions. You agree that contributions to Projects and information about
    contributions may be maintained indefinitely and disclosed publicly, including Your name and other
    information that You submit with Your Submission.
  4. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and
    the parties consent to exclusive jurisdiction and venue in the federal courts sitting in King County,
    Washington, unless no federal subject matter jurisdiction exists, in which case the parties consent to
    exclusive jurisdiction and venue in the Superior Court of King County, Washington. The parties waive all
    defenses of lack of personal jurisdiction and forum non-conveniens.
  5. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and
    supersedes any and all prior agreements, understandings or communications, written or oral, between
    the parties relating to the subject matter hereof. This Agreement may be assigned by Microsoft.

… class docstring

An empty 0x1013 binary property must not mask a populated string variant of
the same property.

@ericsondea-collab ericsondea-collab left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

.msg files saved from Outlook 365 / Exchange lose two things on conversion. Both are reproduced against main below.

1. From is an Exchange DN, not an address. The converter read the sender only from PR_SENDER_EMAIL_ADDRESS (0x0C1F). That property holds an email address only when PR_SENDER_ADDRTYPE (0x0C1E) is SMTP; anything routed through Exchange sets the address type to EX and stores a Distinguished Name there instead, so the header rendered as:

**From:** /O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=ab12cd34

The sender is now resolved in MAPI-correct order — PR_SENDER_SMTP_ADDRESS (0x5D01), then PR_SENT_REPRESENTING_SMTP_ADDRESS (0x5D02), then the legacy properties but only when their address type is not the DN form. If a file genuinely carries nothing else, the DN is still emitted, so no message loses its From header.

2. HTML-only messages render an empty ## Content. The body was read only from PR_BODY (0x1000), which Outlook on the web leaves empty for HTML-only mail — the entire message body was silently dropped. It now falls back to PR_HTML (0x1013) and converts it with the HtmlConverter this repo already ships, the same way PptxConverter / XlsxConverter / EpubConverter delegate. Both the binary (0102) and string (001F) variants of the property are handled, and the byte stream's charset is detected with charset_normalizer — already a core dependency, used the same way in _csv_converter.py and _plain_text_converter.py — rather than assuming UTF-8.

Binary streams are read through a new _read_stream helper rather than _get_stream_data, because that function decodes UTF-16-LE first and arbitrary even-length bytes almost never fail that decode: PR_HTML would come back as mojibake instead of raising. _get_stream_data keeps its exact previous signature and decoding behavior.

No new dependency, and plain-text messages take the identical path they took before — the HTML fallback is reached only when PR_BODY is absent or empty.

Before / after on the message shape described in the issue:

**From:** /O=EXCHANGELABS/OU=.../CN=RECIPIENTS/CN=ab12cd34   ->  **From:** sender@example.com
## Content                                                    ->  ## Content
(empty)                                                              Hello **world**

Testing

New packages/markitdown/tests/test_outlook_msg.py, 11 cases: SMTP-over-DN preference, the sent representing variants, the plain-SMTP case (unchanged behavior), the last-resort DN fallback, HTML-only body conversion, the string-variant property, non-UTF-8 (cp1252) HTML bytes, absent HTML, plain-text-wins-over-HTML, and an end-to-end convert() of the reported message shape.

A .msg file is an OLE2 compound file and nothing in the dependency set can write that container, so a synthetic binary fixture is not possible here the way test_svg_no_fallback.pptx was. The tests instead serve the MAPI streams through a stand-in for olefile.OleFileIO, matching the stub-the-collaborator style already used in tests/test_docintel_html.py and tests/test_cu_converter.py. Happy to swap in a real fixture if a maintainer can share a sanitised Exchange .msg.

pytest tests/test_outlook_msg.py            11 passed
pytest tests/ (full suite)                  347 passed, 3 skipped
black --check .                             73 files unchanged
mypy --ignore-missing-imports               no issues

The existing test_outlook_msg.msg vector is unaffected — that fixture has PR_SENDER_ADDRTYPE == "SMTP", so **From:** test.sender@example.com still holds.

The only full-suite failure, test_module_misc.py::test_speech_transcription, fails identically on a clean checkout of main: the online recogniser returns 1 2 3 4 for a clip the test expects to end in 5. Unrelated to this change.

Fixes #1188

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Outlook converter reads incorrect fields from Outlook 365 (April 2025) files, misses HTML content - Fix included

2 participants