diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py
index 18437c6..b6f7f56 100644
a
|
b
|
from django.conf import settings
|
7 | 7 | from django.core.mail.backends.base import BaseEmailBackend |
8 | 8 | from django.core.mail.utils import DNS_NAME |
9 | 9 | from django.core.mail.message import sanitize_address |
| 10 | from django.utils.encoding import force_bytes |
10 | 11 | |
11 | 12 | |
12 | 13 | class EmailBackend(BaseEmailBackend): |
… |
… |
class EmailBackend(BaseEmailBackend):
|
102 | 103 | from_email = sanitize_address(email_message.from_email, email_message.encoding) |
103 | 104 | recipients = [sanitize_address(addr, email_message.encoding) |
104 | 105 | for addr in email_message.recipients()] |
| 106 | message = email_message.message() |
| 107 | charset = message.get_charset().get_output_charset() if message.get_charset() else 'utf-8' |
105 | 108 | try: |
106 | 109 | self.connection.sendmail(from_email, recipients, |
107 | | email_message.message().as_string()) |
| 110 | force_bytes(message.as_string(), charset)) |
108 | 111 | except: |
109 | 112 | if not self.fail_silently: |
110 | 113 | raise |
diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
index 33898cc..b798cb2 100644
a
|
b
|
from django.core.mail.backends import console, dummy, locmem, filebased, smtp
|
17 | 17 | from django.core.mail.message import BadHeaderError |
18 | 18 | from django.test import TestCase |
19 | 19 | from django.test.utils import override_settings |
| 20 | from django.utils.encoding import force_str, force_text |
20 | 21 | from django.utils.six import PY3, StringIO |
21 | 22 | from django.utils.translation import ugettext_lazy |
22 | 23 | |
… |
… |
class BaseEmailBackendTests(object):
|
357 | 358 | self.assertEqual(message["from"], "from@example.com") |
358 | 359 | self.assertEqual(message.get_all("to"), ["to@example.com"]) |
359 | 360 | |
| 361 | def test_send_unicode(self): |
| 362 | email = EmailMessage('Chère maman', 'Je t\'aime très fort', 'from@example.com', ['to@example.com']) |
| 363 | num_sent = mail.get_connection().send_messages([email]) |
| 364 | self.assertEqual(num_sent, 1) |
| 365 | message = self.get_the_message() |
| 366 | self.assertEqual(message["subject"], '=?utf-8?q?Ch=C3=A8re_maman?=') |
| 367 | self.assertEqual(force_text(message.get_payload()), 'Je t\'aime très fort') |
| 368 | |
360 | 369 | def test_send_many(self): |
361 | 370 | email1 = EmailMessage('Subject', 'Content1', 'from@example.com', ['to@example.com']) |
362 | 371 | email2 = EmailMessage('Subject', 'Content2', 'from@example.com', ['to@example.com']) |
… |
… |
class FileBackendTests(BaseEmailBackendTests, TestCase):
|
526 | 535 | messages = [] |
527 | 536 | for filename in os.listdir(self.tmp_dir): |
528 | 537 | with open(os.path.join(self.tmp_dir, filename), 'r') as fp: |
529 | | session = fp.read().split('\n' + ('-' * 79) + '\n') |
530 | | messages.extend(email.message_from_string(str(m)) for m in session if m) |
| 538 | session = force_text(fp.read()).split('\n' + ('-' * 79) + '\n') |
| 539 | messages.extend(email.message_from_string(force_str(m)) for m in session if m) |
531 | 540 | return messages |
532 | 541 | |
533 | 542 | def test_file_sessions(self): |
… |
… |
class ConsoleBackendTests(BaseEmailBackendTests, TestCase):
|
579 | 588 | self.stream = sys.stdout = StringIO() |
580 | 589 | |
581 | 590 | def get_mailbox_content(self): |
582 | | messages = self.stream.getvalue().split('\n' + ('-' * 79) + '\n') |
583 | | return [email.message_from_string(str(m)) for m in messages if m] |
| 591 | messages = force_text(self.stream.getvalue()).split('\n' + ('-' * 79) + '\n') |
| 592 | return [email.message_from_string(force_str(m)) for m in messages if m] |
584 | 593 | |
585 | 594 | def test_console_stream_kwarg(self): |
586 | 595 | """ |