classification
Title: email.policy.SMTP throws AttributeError on invalid header
Type: behavior Stage: patch review
Components: email Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, barry, elenril, r.david.murray
Priority: normal Keywords: patch

Created on 2019-12-19 19:32 by elenril, last changed 2021-07-07 08:14 by elenril.

Files
File name Uploaded Description Edit
mail.eml elenril, 2019-12-19 19:32
Pull Requests
URL Status Linked Edit
PR 24872 open elenril, 2021-03-15 11:22
Messages (7)
msg358689 - (view) Author: Anton Khirnov (elenril) * Date: 2019-12-19 19:32
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.
msg396905 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-07-03 14:03
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.
msg397046 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2021-07-06 16:59
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)
msg397047 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-07-06 17:10
Running R.David's test script on python3.7, it also works, there's no exception.
msg397048 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-07-06 17:11
I should have been more exact - I've used python3.7.7 .
msg397065 - (view) Author: Anton Khirnov (elenril) * Date: 2021-07-07 08:11
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'])
msg397066 - (view) Author: Anton Khirnov (elenril) * Date: 2021-07-07 08:14
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.
History
Date User Action Args
2021-07-07 08:14:24elenrilsetmessages: + msg397066
2021-07-07 08:11:59elenrilsetmessages: + msg397065
2021-07-06 17:11:32andrei.avksetmessages: + msg397048
2021-07-06 17:10:28andrei.avksetmessages: + msg397047
2021-07-06 16:59:56r.david.murraysetmessages: + msg397046
2021-07-03 14:03:34andrei.avksetnosy: + andrei.avk
messages: + msg396905
2021-03-15 11:22:23elenrilsetkeywords: + patch
stage: patch review
pull_requests: + pull_request23633
2019-12-19 19:32:14elenrilcreate