Skip to content

email.policy.SMTP throws AttributeError on invalid header #83281

Closed

Description

@elenril
mannequin
BPO 39100
Nosy @warsaw, @bitdancer, @elenril, @akulakov
PRs
  • bpo-39100: _header_value_parser: do not treat a Group as invalid-mailbox #24872
  • Files
  • mail.eml
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-12-19.19:32:14.506>
    labels = ['type-bug', 'expert-email']
    title = 'email.policy.SMTP throws AttributeError on invalid header'
    updated_at = <Date 2022-01-30.14:52:51.302>
    user = 'https://github.com/elenril'

    bugs.python.org fields:

    activity = <Date 2022-01-30.14:52:51.302>
    actor = 'andrei.avk'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['email']
    creation = <Date 2019-12-19.19:32:14.506>
    creator = 'elenril'
    dependencies = []
    files = ['48792']
    hgrepos = []
    issue_num = 39100
    keywords = ['patch']
    message_count = 9.0
    messages = ['358689', '396905', '397046', '397047', '397048', '397065', '397066', '412141', '412152']
    nosy_count = 4.0
    nosy_names = ['barry', 'r.david.murray', 'elenril', 'andrei.avk']
    pr_nums = ['24872']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue39100'
    versions = []

    Activity

    1. elenril commented on Dec 19, 2019

      elenrilmannequin
      MannequinAuthor

      When parsing a (broken) mail from linux-media@vger.kernel.org (message-id 20190212181908.Horde.pEiGHvV2KHy9EkUy8TA8D1o@webmail.your-server.de, headers attached) with email.policy.SMTP, I get an AttributeError on trying to read the 'to' header:

      /usr/lib/python3.7/email/headerregistry.py in <listcomp>(.0)
      345 mb.local_part or '',
      346 mb.domain or '')
      --> 347 for mb in addr.all_mailboxes]))
      348 defects = list(address_list.all_defects)
      349 else:

      AttributeError: 'Group' object has no attribute 'local_part'

      The header in question is:
      To: unlisted-recipients:; (no To-header on input)

      The problem seems to be that mb is a Group and not an Address, gets token_type of 'invalid-mailbox', but does not have the attributes local_part/domain that are expected in mailboxes. Copying the line

      local_part = domain = route = addr_spec = display_name

      from InvalidMailbox to Group fixes this, but it is not clear to me this is the right solution, so not sending a patch.

    2. akulakov commented on Jul 3, 2021

      @akulakov
      Contributor

      Anton: thanks for the report! In the message you say you are not sure of the solution, and because of that not sending a patch, but then you created the PR; - please clarify.

    3. bitdancer commented on Jul 6, 2021

      @bitdancer
      Member

      How are you encountering this error? The following program runs without exception for me on master:

      from email import message_from_binary_file
      from email.policy import SMTP
      
      msg = message_from_binary_file(open('mail.eml', 'rb'), policy=SMTP)
      print(msg)
    4. akulakov commented on Jul 6, 2021

      @akulakov
      Contributor

      Running R.David's test script on python3.7, it also works, there's no exception.

    5. akulakov commented on Jul 6, 2021

      @akulakov
      Contributor

      I should have been more exact - I've used python3.7.7 .

    6. elenril commented on Jul 7, 2021

      elenrilmannequin
      MannequinAuthor

      Quoting R. David Murray (2021-07-06 18:59:56)

      How are you encountering this error? The following program runs without exception for me on master:

      from email import message_from_binary_file
      from email.policy import SMTP

      msg = message_from_binary_file(open('mail.eml', 'rb'), policy=SMTP)
      print(msg)

      The exception is thrown on accessing the 'to' header, so make that
      print(msg['to'])

    7. elenril commented on Jul 7, 2021

      elenrilmannequin
      MannequinAuthor

      Quoting Andrei Kulakov (2021-07-03 16:03:34)

      Anton: thanks for the report! In the message you say you are not sure
      of the solution, and because of that not sending a patch, but then you
      created the PR; - please clarify.

      I didn't send a patch when originally submitting the bugreport in 2019.
      I did write a patch about a year later, since there was no response to
      the bugreport.

    8. elenril commented on Jan 30, 2022

      elenrilmannequin
      MannequinAuthor

      Ping yet again. Can anyone please look at this?

    9. akulakov commented on Jan 30, 2022

      @akulakov
      Contributor

      I confirmed I get the same error as Anton on 3.9 and 3.11 .

    10. transferred this issue fromon Apr 10, 2022
    11. ekamioka commented on Jun 28, 2022

      @ekamioka

      Also confirmed. Getting the same error as Anton on python 3.9.13_1 and 3.8.10

    12. rushkii commented on Nov 1, 2022

      @rushkii

      This error has not been handled yet? I've got the same error taken from the lore.kerne.org mailing list.
      Prettified email
      Raw email

    13. slverpla commented on Jul 21, 2023

      @slverpla

      Hi, also getting this error. Is there anything I can do to help resolve this? Or did anyone find a workaround?

    14. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Nov 23, 2023
    15. aseering commented on Dec 27, 2023

      @aseering

      I'm hitting this as well. It looks like the PR with the fix ( #24872 ) is slowly moving forward?

    16. SuddenlyCoding commented on Apr 28, 2026

      @SuddenlyCoding

      Hit this when serializing emails parsed from Outlook PSTs (recipients via distribution lists produce group syntax in To: headers).

      Workaround: pre-process address-list headers with the legacy email.utils.getaddresses parser, which flattens groups correctly, and write the headers back in flat-mailbox form before calling as_bytes(policy=default).

    17. bitdancer commented on Apr 30, 2026

      @bitdancer
      Member

      Thanks for the ping, and thanks elenril for the PR. Apologies to all for the delay in getting it committed.

    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