mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #27007 -- Handled non-UTF-8 bytes objects for text/* attachments.
The fallback logic which allows non-UTF-8 encoded files to be passed to attach_file() even when a `text/*` mime type has been specified is moved to attach(). Both functions now fall back to a content type of `application/octet-stream`. A side effect is that a file's content is decoded in memory instead of opening it in text mode and reading it into a string. Some mimetype-related logic in _create_attachment() has become obsolete as the code moved from attach_file() to attach() already handles this.
This commit is contained in:
committed by
Tim Graham
parent
311a8e8d50
commit
72d541b61c
@@ -422,6 +422,31 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
|
||||
self.assertEqual(content, b'file content')
|
||||
self.assertEqual(mimetype, 'text/plain')
|
||||
|
||||
def test_attach_utf8_text_as_bytes(self):
|
||||
"""
|
||||
Non-ASCII characters encoded as valid UTF-8 are correctly transported
|
||||
and decoded.
|
||||
"""
|
||||
msg = EmailMessage('subject', 'body', 'from@example.com', ['to@example.com'])
|
||||
msg.attach('file.txt', b'\xc3\xa4') # UTF-8 encoded a umlaut.
|
||||
filename, content, mimetype = self.get_decoded_attachments(msg)[0]
|
||||
self.assertEqual(filename, 'file.txt')
|
||||
self.assertEqual(content, b'\xc3\xa4')
|
||||
self.assertEqual(mimetype, 'text/plain')
|
||||
|
||||
def test_attach_non_utf8_text_as_bytes(self):
|
||||
"""
|
||||
Binary data that can't be decoded as UTF-8 overrides the MIME type
|
||||
instead of decoding the data.
|
||||
"""
|
||||
msg = EmailMessage('subject', 'body', 'from@example.com', ['to@example.com'])
|
||||
msg.attach('file.txt', b'\xff') # Invalid UTF-8.
|
||||
filename, content, mimetype = self.get_decoded_attachments(msg)[0]
|
||||
self.assertEqual(filename, 'file.txt')
|
||||
# Content should be passed through unmodified.
|
||||
self.assertEqual(content, b'\xff')
|
||||
self.assertEqual(mimetype, 'application/octet-stream')
|
||||
|
||||
def test_dummy_backend(self):
|
||||
"""
|
||||
Make sure that dummy backends returns correct number of sent messages
|
||||
|
||||
Reference in New Issue
Block a user