Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

email.policy.SMTP TypeError: 'Header' object is not subscriptable #100261

Open
barskykd opened this issue Dec 15, 2022 · 4 comments
Open

email.policy.SMTP TypeError: 'Header' object is not subscriptable #100261

barskykd opened this issue Dec 15, 2022 · 4 comments
Labels
topic-email type-bug An unexpected behavior, bug, or error

Comments

@barskykd
Copy link

Bug report

Code snippet is taken directly from documentation https://docs.python.org/3/library/email.header.html
The only change - policy changed to email.policy.SMTP.
Documentation (https://docs.python.org/3/library/email.policy.html#email.policy.SMTP) states that the only difference between email.policy.default and email.policy.SMTP - linesep used.

from email.message import Message
from email.header import Header
from email.policy import SMTP

msg = Message()
h = Header('p\xf6stal', 'iso-8859-1')
msg['Subject'] = h
print(msg.as_string())

msg = Message(policy=SMTP)
h = Header('p\xf6stal', 'iso-8859-1')
msg['Subject'] = h
print(msg.as_string())

Output is:

Subject: =?iso-8859-1?q?p=F6stal?=


Traceback (most recent call last):
  File "C:\Users\barsk\AppData\Roaming\JetBrains\PyCharm2022.3\scratches\scratch_3.py", line 12, in <module>
    msg['Subject'] = h
  File "C:\Program Files (x86)\Python37-32\lib\email\message.py", line 409, in __setitem__
    self._headers.append(self.policy.header_store_parse(name, val))
  File "C:\Program Files (x86)\Python37-32\lib\email\policy.py", line 148, in header_store_parse
    return (name, self.header_factory(name, value))
  File "C:\Program Files (x86)\Python37-32\lib\email\headerregistry.py", line 589, in __call__
    return self[name](name, value)
  File "C:\Program Files (x86)\Python37-32\lib\email\headerregistry.py", line 197, in __new__
    cls.parse(value, kwds)
  File "C:\Program Files (x86)\Python37-32\lib\email\headerregistry.py", line 272, in parse
    kwds['parse_tree'] = cls.value_parser(value)
  File "C:\Program Files (x86)\Python37-32\lib\email\_header_value_parser.py", line 1105, in get_unstructured
    if value[0] in WSP:
TypeError: 'Header' object is not subscriptable

Your environment

Reproduced on windows on

  • Python 3.11 (64-bit)
  • Python 3.10 (64-bit)
  • Python 3.7 (32-bit)
@barskykd barskykd added the type-bug An unexpected behavior, bug, or error label Dec 15, 2022
@codzy-ds
Copy link

I have the same bug but for the policy.default.

Is there a workaround? It is quite a blocker.

@barskykd
Copy link
Author

For this particular bug there is workaround. Convert Header to string before assignment

msg['Subject'] = str(h)

@codzy-ds
Copy link

Oh, simple enough, thank you!

@alex-pobeditel-2004
Copy link

alex-pobeditel-2004 commented Jan 11, 2023

I've found this bug while fighting with problem described here: #44510

When I create an email with Subject as str, spaces that are located near email body line breaks are lost during decode/encode. For example:

msg['Subject'] = 'Приглашение в закрытую программу Перенос удалится'

gets encoded as:

Subject: =?utf-8?b?0J/RgNC40LPQu9Cw0YjQtdC90LjQtSDQsiDQt9Cw0LrRgNGL0YLRg9GO?=
  =?utf-8?b?0L/RgNC+0LPRgNCw0LzQvNGDINCf0LXRgNC10L3QvtGBINGD0LTQsNC70LjRgtGB?=
 =?utf-8?q?=D1=8F?=

and later is decoded by email client in this way:

Subject: Приглашение в закрытуюпрограмму Перенос удалится

There are 2 spaces in the beginning of line 2 of MIME representation, but on decode the "significant" space is lost.

If it's impossible now to declare header in this way (what was recommended in the bug mentioned above):

msg['Subject'] = Header('Приглашение в закрытую программу Перенос удалится', 'utf-8')

Is there a workaround for such cases?

I figured out that it's possible to use longer "raw" lines via email policy (max_line_length=998, for example), but I'm not sure this is the most correct way to overcome this problem.

UPD: No, setting high max_line_length does not affect the Subject header.

UPD2: Looks like my problem can't be solved with Header either, I found the exact same bug here: #92081

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-email type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants