Ticket #19186: 19186-1.diff

File 19186-1.diff, 3.7 KB (added by claudep, 3 years ago)

Encode message before submitting it to sendmail

  • django/core/mail/backends/smtp.py

    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 
    77from django.core.mail.backends.base import BaseEmailBackend
    88from django.core.mail.utils import DNS_NAME
    99from django.core.mail.message import sanitize_address
     10from django.utils.encoding import force_bytes
    1011
    1112
    1213class EmailBackend(BaseEmailBackend):
    class EmailBackend(BaseEmailBackend): 
    102103        from_email = sanitize_address(email_message.from_email, email_message.encoding)
    103104        recipients = [sanitize_address(addr, email_message.encoding)
    104105                      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'
    105108        try:
    106109            self.connection.sendmail(from_email, recipients,
    107                     email_message.message().as_string())
     110                    force_bytes(message.as_string(), charset))
    108111        except:
    109112            if not self.fail_silently:
    110113                raise
  • tests/regressiontests/mail/tests.py

    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 
    1717from django.core.mail.message import BadHeaderError
    1818from django.test import TestCase
    1919from django.test.utils import override_settings
     20from django.utils.encoding import force_str, force_text
    2021from django.utils.six import PY3, StringIO
    2122from django.utils.translation import ugettext_lazy
    2223
    class BaseEmailBackendTests(object): 
    357358        self.assertEqual(message["from"], "from@example.com")
    358359        self.assertEqual(message.get_all("to"), ["to@example.com"])
    359360
     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
    360369    def test_send_many(self):
    361370        email1 = EmailMessage('Subject', 'Content1', 'from@example.com', ['to@example.com'])
    362371        email2 = EmailMessage('Subject', 'Content2', 'from@example.com', ['to@example.com'])
    class FileBackendTests(BaseEmailBackendTests, TestCase): 
    526535        messages = []
    527536        for filename in os.listdir(self.tmp_dir):
    528537            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)
    531540        return messages
    532541
    533542    def test_file_sessions(self):
    class ConsoleBackendTests(BaseEmailBackendTests, TestCase): 
    579588        self.stream = sys.stdout = StringIO()
    580589
    581590    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]
    584593
    585594    def test_console_stream_kwarg(self):
    586595        """
Back to Top