Skip to content

bug/email-non-ascii-addresses: sender and recipient names come out RFC 2047-encoded, non-ASCII addresses fail the message #4542

Closed

Description

@usmanmateen

Describe the bug

partition_email() formats the sent_from, sent_to, cc_recipient and bcc_recipient metadata with email.utils.formataddr(). Since the message is parsed with email.policy.default, the headers are already decoded, and formataddr() then:

  1. writes any non-ASCII display name as an RFC 2047 encoded-word, so the metadata has =?utf-8?b?5byg5Lyf?= <[email protected]> instead of 张伟 <[email protected]>. Every mail client writes non-ASCII names this way on the wire, so this affects most mail with an accented or non-Latin sender or recipient name. The subject of the same message is decoded correctly;
  2. raises UnicodeEncodeError on a non-ASCII address, which RFC 6532 (SMTPUTF8) allows, e.g. jü[email protected]. This fails the whole message, not just the metadata field.

To Reproduce

import email.policy
import io
from email.message import EmailMessage

from unstructured.partition.email import partition_email

# 1. non-ASCII display names, RFC 2047-encoded on the wire as mail clients do
msg = EmailMessage(policy=email.policy.SMTP)
msg["From"] = "José García <[email protected]>"
msg["To"] = "张伟 <[email protected]>"
msg["Subject"] = "Grüße"
msg.set_content("Hello")
element = partition_email(file=io.BytesIO(msg.as_bytes()))[0]
print(element.metadata.sent_from, element.metadata.sent_to, element.metadata.subject)
# ['=?utf-8?b?Sm9zw6kgR2FyY8OtYQ==?= <[email protected]>'] ['=?utf-8?b?5byg5Lyf?= <[email protected]>'] Grüße

# 2. a non-ASCII address (RFC 6532)
msg = EmailMessage(policy=email.policy.SMTPUTF8)
msg["From"] = "José <josé@ejemplo.es>"
msg["To"] = "[email protected]"
msg.set_content("Hello")
partition_email(file=io.BytesIO(msg.as_bytes()))
# UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 3: ordinal not in range(128)
#   File "unstructured/partition/email.py", line 187, in from_address
#     formatted_addrs = [email.utils.formataddr(addr) for addr in addrs]

Expected behavior

The names and addresses as written: ['José García <[email protected]>'], ['张伟 <[email protected]>'] and ['José <josé@ejemplo.es>']. ASCII names and addresses should keep their current format, including the quoting of names like "Doe, John" <[email protected]>.

Environment Info

scripts/collect_env.py needs pkg_resources, which isn't installed here, so by hand: main at e2f22b3 (0.27.26), Python 3.12.13, Linux on WSL2 (glibc 2.43).

Additional context

I found the address crash with tests/mails/mail_test_12 from SpamScope/mail-parser, whose To: header has a raw 8-bit address. A small helper that quotes and escapes the display name the way formataddr() does, without the encoding step, fixes both. I have it with tests and will open a PR.

Activity

  1. added a commit that references this issue on Oct 19, 2026
    08d407e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions