mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
Handle unicode attachment content in both python 2.X and python3.
This commit is contained in:
@@ -294,15 +294,18 @@ class DjrillBackend(BaseEmailBackend):
|
|||||||
if mimetype is None:
|
if mimetype is None:
|
||||||
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
|
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
|
||||||
|
|
||||||
|
# b64encode requires bytes, so let's convert our content.
|
||||||
try:
|
try:
|
||||||
content_b64 = b64encode(content)
|
if isinstance(content, unicode):
|
||||||
except TypeError:
|
# Python 2.X unicode string
|
||||||
# Python 3 b64encode requires bytes. Convert str attachment:
|
content = content.encode(str_encoding)
|
||||||
|
except NameError:
|
||||||
|
# Python 3 doesn't differentiate between strings and unicode
|
||||||
|
# Convert python3 unicode str to bytes attachment:
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
content_bytes = content.encode(str_encoding)
|
content = content.encode(str_encoding)
|
||||||
content_b64 = b64encode(content_bytes)
|
|
||||||
else:
|
content_b64 = b64encode(content)
|
||||||
raise
|
|
||||||
|
|
||||||
mandrill_attachment = {
|
mandrill_attachment = {
|
||||||
'type': mimetype,
|
'type': mimetype,
|
||||||
|
|||||||
@@ -180,18 +180,17 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase):
|
|||||||
self.assertFalse('images' in data['message'])
|
self.assertFalse('images' in data['message'])
|
||||||
|
|
||||||
def test_unicode_attachment_correctly_decoded(self):
|
def test_unicode_attachment_correctly_decoded(self):
|
||||||
unicode_attachment = [
|
msg = mail.EmailMessage(
|
||||||
('before_html.html', u'<p>\u2019</p>', 'text/html'),
|
|
||||||
]
|
|
||||||
email = mail.EmailMessage(
|
|
||||||
subject='Subject',
|
subject='Subject',
|
||||||
body='Body goes here',
|
body='Body goes here',
|
||||||
from_email='from@example.com',
|
from_email='from@example.com',
|
||||||
to=['to1@example.com'],
|
to=['to1@example.com'],
|
||||||
attachments=unicode_attachment,
|
|
||||||
)
|
)
|
||||||
|
# Slight modification from the Django unicode docs:
|
||||||
|
# http://django.readthedocs.org/en/latest/ref/unicode.html#email
|
||||||
|
msg.attach("Une pièce jointe.html", u'<p>\u2019</p>', mimetype='text/html')
|
||||||
|
|
||||||
email.send()
|
msg.send()
|
||||||
data = self.get_api_call_data()
|
data = self.get_api_call_data()
|
||||||
|
|
||||||
attachments = data['message']['attachments']
|
attachments = data['message']['attachments']
|
||||||
|
|||||||
Reference in New Issue
Block a user