Skip to content

Missing 'linear-white-space' between encoded-word in RFC2047 header #144156

Description

@guewen

Bug report

Bug description:

import email.policy
from email.message import EmailMessage

message = EmailMessage(policy=email.policy.SMTP)
subject = "Re: [SOS-1495488] Commande et livraison - Demande de retour - bibijolie - 251210-AABBCC - Abo actualités digitales 20 semaines d’abonnement à 24 heures, Bilan, Tribune de Genève et tous les titres Tamedia"
message["Subject"] = subject
print(message.as_bytes())

result

Subject: Re: [SOS-1495488] Commande et livraison - Demande de retour -\r\n bibijolie - 251210-AABBCC - Abo =?utf-8?q?actualit=C3=A9s?= digitales 20\r\n semaines =?utf-8?q?d=E2=80=99abonnement_=C3=A0?= 24 heures, Bilan, Tribune de\r\n =?utf-8?q?_?==?utf-8?q?Gen=C3=A8ve?= et tous les titres Tamedia\r\n\r\n

When sending an email with this subject, yahoo rejects it with a soft-bounce

<xxxxx@yahoo.com>: host mta7.am0.yahoodns.net[67.195.228.111] said: 554
    Message not accepted. Invalid Subject header. See
    https://senders.yahooinc.com/smtp-error-codes#other-failures (in reply to
    end of DATA command)

In the last line of the subject, two encoded-words are appended together ?==? not complying to this part of the RFC2047

Ordinary ASCII text and 'encoded-word's may appear together in the
same header field. However, an 'encoded-word' that appears in a
header field defined as '*text' MUST be separated from any adjacent
'encoded-word' or 'text' by 'linear-white-space'.

Also, I'm not sure that it has to create an encoded-word for this =?utf-8?q?_?=, but I'm a bit lost in the specs.

It is highly dependent to the number of chars and where the chars that need encoding are placed, as the same subject which does not start with "Re: " is fine

Subject: [SOS-1495488] Commande et livraison - Demande de retour - bibijolie -\r\n 251210-AABBCC - Abo =?utf-8?q?actualit=C3=A9s_digitales_20_semaines_d?=\r\n =?utf-8?q?=E2=80=99abonnement_=C3=A0_24_heures=2C_Bilan=2C_Tribune_de_Gen?=\r\n =?utf-8?q?=C3=A8ve?= et tous les titres Tamedia\r\n\r\n

I could reproduce this behavior on 3.13.x, 3.14.2, 3.15-dev

Apologies if there already is an issue for this, I found several issues on the topic, but could not find a matching one.

CPython versions tested on:

CPython main branch, 3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jan 22, 2026
  2. Julien00859 commented on Jan 30, 2026

    @Julien00859

    This issue is impacting the delivery of emailS to Yahoo. Yahoo has a strict policy regarding RFC2047 encoding and is rejecting the emails that are tripping on this issue. It doesn't seem we can do much about to mitigate this problem, beside disabling folding.

  3. guewen commented on Feb 3, 2026

    @guewen
    Author

    Checked more versions, based on this script

    #!/usr/bin/env python3
    
    import email.policy
    import sys
    from email.message import EmailMessage
    
    message = EmailMessage(policy=email.policy.SMTP)
    subject = "Re: [SOS-1495488] Commande et livraison - Demande de retour - bibijolie - 251210-AABBCC - Abo actualités digitales 20 semaines d’abonnement à 24 heures, Bilan, Tribune de Genève et tous les titres Tamedia"
    message["Subject"] = subject
    
    if b"?==?" in message.as_bytes():
        print(f"Python {sys.version}: ❌")
    else:
        print(f"Python {sys.version}: ✅")

    Result

    ❯ for version in 3.7.17 3.8.20 3.9.25 3.10.19 3.11.14 3.12.12 3.13.11 3.14.2 3.15-dev; do mise exec python@$version -- python /tmp/check_email.py; done
    Python 3.7.17 (default, Feb  3 2026, 15:19:23) 
    [GCC 15.2.1 20260103]: ✅
    Python 3.8.20 (default, Oct  2 2024, 16:34:12) 
    [Clang 18.1.8 ]: ✅
    Python 3.9.25 (main, Oct 31 2025, 23:00:23) 
    [Clang 21.1.4 ]: ✅
    Python 3.10.19 (main, Jan 27 2026, 23:59:05) [Clang 21.1.4 ]: ✅
    Python 3.11.14 (main, Jan 27 2026, 23:58:49) [Clang 21.1.4 ]: ✅
    Python 3.12.12 (main, Jan 27 2026, 23:58:14) [Clang 21.1.4 ]: ❌
    Python 3.13.11 (main, Jan 28 2026, 00:01:45) [Clang 21.1.4 ]: ❌
    Python 3.14.2 (main, Dec 17 2025, 21:08:09) [Clang 21.1.4 ]: ❌
    Python 3.15.0a5+ (heads/main:6181b69, Jan 22 2026, 09:30:43) [GCC 15.2.1 20260103]: ❌
  4. robsdedude commented on Feb 8, 2026

    @robsdedude
    Contributor

    I think this might be the PR that introduced the change in behavior gh-92281

  5. added a commit that references this issue on Feb 19, 2026
  6. added a commit that references this issue on Feb 19, 2026
  7. added 2 commits that reference this issue on Feb 19, 2026
  8. bitdancer commented on Feb 24, 2026

    @bitdancer
    Member

    Thanks all, the fix is now merged and backported.

  9. added 2 commits that reference this issue on Feb 25, 2026
  10. added 2 commits that reference this issue on Feb 25, 2026
  11. added 2 commits that reference this issue on Feb 28, 2026
  12. added 2 commits that reference this issue on Apr 25, 2026
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

    stdlibStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions