Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 3 Lib/email/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ def _write(self, msg):
# If we munged the cte, copy the message again and re-fix the CTE.
if munge_cte:
msg = deepcopy(msg)
msg.replace_header('content-transfer-encoding', munge_cte[0])
msg.replace_header('content-type', munge_cte[1])
if msg.get('content-transfer-encoding') is not None:
msg.replace_header('content-transfer-encoding', munge_cte[0])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you set the header if it doesn't already exists?

See #1977 (comment)

# Write the headers. First we see if the message object wants to
# handle that itself. If not, we'll do it generically.
meth = getattr(msg, '_write_headers', None)
Expand Down
28 changes: 28 additions & 0 deletions 28 Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ def test_as_string_policy(self):
g.flatten(msg)
self.assertEqual(fullrepr, s.getvalue())

def test_nonascii_as_string_without_cte(self):
m = textwrap.dedent("""\
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----5F0D91A9C8C24D91AF71E1D7F2F7877E"

------5F0D91A9C8C24D91AF71E1D7F2F7877E
Content-type: text/plain; charset=utf-8

Test if UTF-8 messages with no Content-Transfer-Encoding set can be as_string'd:
Föö bär
------5F0D91A9C8C24D91AF71E1D7F2F7877E--

""")
msg = email.message_from_string(m)
msg.as_string()

def test_nonascii_as_string_without_content_type_and_cte(self):
m = textwrap.dedent("""\
MIME-Version: 1.0

Test if UTF-8 messages with no Content-Type nor Content-Transfer-Encoding set can be as_string'd:
Föö bär

""")
msg = email.message_from_string(m)
msg.as_string()

def test_as_bytes(self):
msg = self._msgobj('msg_01.txt')
with openfile('msg_01.txt') as fp:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Only replace the munged Content-Type-Encoding header when it actually exists
while flattening an email.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.