Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #19186 -- Fixed sending mail with unicode content on Python 3

Thanks alex_po for the report and Luke Plant for the analysis.
  • Loading branch information...
commit 1620c27936718a87bbc8acce7886ef20fee2b3fe 1 parent 1e34fd3
@claudep claudep authored
View
5 django/core/mail/backends/smtp.py
@@ -7,6 +7,7 @@
from django.core.mail.backends.base import BaseEmailBackend
from django.core.mail.utils import DNS_NAME
from django.core.mail.message import sanitize_address
+from django.utils.encoding import force_bytes
class EmailBackend(BaseEmailBackend):
@@ -102,9 +103,11 @@ def _send(self, email_message):
from_email = sanitize_address(email_message.from_email, email_message.encoding)
recipients = [sanitize_address(addr, email_message.encoding)
for addr in email_message.recipients()]
+ message = email_message.message()
+ charset = message.get_charset().get_output_charset() if message.get_charset() else 'utf-8'
try:
self.connection.sendmail(from_email, recipients,
- email_message.message().as_string())
+ force_bytes(message.as_string(), charset))
except:
if not self.fail_silently:
raise
View
17 tests/regressiontests/mail/tests.py
@@ -17,6 +17,7 @@
from django.core.mail.message import BadHeaderError
from django.test import TestCase
from django.test.utils import override_settings
+from django.utils.encoding import force_str, force_text
from django.utils.six import PY3, StringIO
from django.utils.translation import ugettext_lazy
@@ -357,6 +358,14 @@ def test_send(self):
self.assertEqual(message["from"], "from@example.com")
self.assertEqual(message.get_all("to"), ["to@example.com"])
+ def test_send_unicode(self):
+ email = EmailMessage('Chère maman', 'Je t\'aime très fort', 'from@example.com', ['to@example.com'])
+ num_sent = mail.get_connection().send_messages([email])
+ self.assertEqual(num_sent, 1)
+ message = self.get_the_message()
+ self.assertEqual(message["subject"], '=?utf-8?q?Ch=C3=A8re_maman?=')
+ self.assertEqual(force_text(message.get_payload()), 'Je t\'aime très fort')
+
def test_send_many(self):
email1 = EmailMessage('Subject', 'Content1', 'from@example.com', ['to@example.com'])
email2 = EmailMessage('Subject', 'Content2', 'from@example.com', ['to@example.com'])
@@ -526,8 +535,8 @@ def get_mailbox_content(self):
messages = []
for filename in os.listdir(self.tmp_dir):
with open(os.path.join(self.tmp_dir, filename), 'r') as fp:
- session = fp.read().split('\n' + ('-' * 79) + '\n')
- messages.extend(email.message_from_string(str(m)) for m in session if m)
+ session = force_text(fp.read()).split('\n' + ('-' * 79) + '\n')
+ messages.extend(email.message_from_string(force_str(m)) for m in session if m)
return messages
def test_file_sessions(self):
@@ -579,8 +588,8 @@ def flush_mailbox(self):
self.stream = sys.stdout = StringIO()
def get_mailbox_content(self):
- messages = self.stream.getvalue().split('\n' + ('-' * 79) + '\n')
- return [email.message_from_string(str(m)) for m in messages if m]
+ messages = force_text(self.stream.getvalue()).split('\n' + ('-' * 79) + '\n')
+ return [email.message_from_string(force_str(m)) for m in messages if m]
def test_console_stream_kwarg(self):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.