Skip to content

Commit a3185da

Browse files
bpo-41402: Fix email ContentManager calling .encode() on bytes (GH-21631)
(cherry picked from commit b33186b) Co-authored-by: Johannes Reiff <[email protected]>
1 parent 8ece98a commit a3185da

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Lib/email/contentmanager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ def set_bytes_content(msg, data, maintype, subtype, cte='base64',
238238
data = binascii.b2a_qp(data, istext=False, header=False, quotetabs=True)
239239
data = data.decode('ascii')
240240
elif cte == '7bit':
241-
# Make sure it really is only ASCII. The early warning here seems
242-
# worth the overhead...if you care write your own content manager :).
243-
data.encode('ascii')
241+
data = data.decode('ascii')
244242
elif cte in ('8bit', 'binary'):
245243
data = data.decode('ascii', 'surrogateescape')
246244
msg.set_payload(data)

Lib/test/test_email/test_contentmanager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,18 @@ def test_set_non_ascii_filename(self):
776776
foo
777777
""").encode('ascii'))
778778

779+
def test_set_content_bytes_cte_7bit(self):
780+
m = self._make_message()
781+
m.set_content(b'ASCII-only message.\n',
782+
maintype='application', subtype='octet-stream', cte='7bit')
783+
self.assertEqual(str(m), textwrap.dedent("""\
784+
Content-Type: application/octet-stream
785+
Content-Transfer-Encoding: 7bit
786+
MIME-Version: 1.0
787+
788+
ASCII-only message.
789+
"""))
790+
779791
content_object_params = {
780792
'text_plain': ('content', ()),
781793
'text_html': ('content', ('html',)),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :meth:`email.message.EmailMessage.set_content` when called with binary data and ``7bit`` content transfer encoding.

0 commit comments

Comments
 (0)