diff --git a/django/core/mail/message.py b/django/core/mail/message.py
index 98ab3c9..5702b2e 100644
a
|
b
|
from django.utils import six
|
21 | 21 | |
22 | 22 | # Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from |
23 | 23 | # some spam filters. |
24 | | Charset.add_charset('utf-8', Charset.SHORTEST, None, 'utf-8') |
| 24 | utf8_charset = Charset.Charset('utf-8') |
| 25 | utf8_charset.body_encoding = None # Python defaults to BASE64 |
25 | 26 | |
26 | 27 | # Default MIME type to use on attachments (if it is not explicitly given |
27 | 28 | # and cannot be guessed). |
… |
… |
class SafeMIMEText(MIMEText):
|
123 | 124 | |
124 | 125 | def __init__(self, text, subtype, charset): |
125 | 126 | self.encoding = charset |
126 | | MIMEText.__init__(self, text, subtype, charset) |
| 127 | MIMEText.__init__(self, text, subtype, None) |
| 128 | # Unfortunately, Python doesn't support setting a Charset instance as |
| 129 | # MIMEText init parameter (http://bugs.python.org/issue16324) |
| 130 | if charset == 'utf-8': |
| 131 | self.set_charset(utf8_charset) |
| 132 | else: |
| 133 | self.set_charset(charset) |
| 134 | self.replace_header('Content-Type', 'text/%s; charset="%s"' % (subtype, charset)) |
127 | 135 | |
128 | 136 | def __setitem__(self, name, val): |
129 | 137 | name, val = forbid_multi_line_headers(name, val, self.encoding) |
diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
index 33898cc..dcc8b3a 100644
a
|
b
|
class MailTests(TestCase):
|
89 | 89 | """ |
90 | 90 | headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} |
91 | 91 | email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers) |
92 | | self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent') |
| 92 | self.assertEqual(email.message().as_string(), 'MIME-Version: 1.0\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent') |
93 | 93 | |
94 | 94 | def test_from_header(self): |
95 | 95 | """ |
… |
… |
class MailTests(TestCase):
|
171 | 171 | email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'from@example.com', ['other@example.com']) |
172 | 172 | email.encoding = 'iso-8859-1' |
173 | 173 | message = email.message() |
174 | | self.assertTrue(message.as_string().startswith('Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com')) |
| 174 | self.assertTrue(message.as_string().startswith('MIME-Version: 1.0\nContent-Type: text/plain; charset="iso-8859-1"\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com')) |
175 | 175 | self.assertEqual(message.get_payload(), 'Firstname S=FCrname is a great guy.') |
176 | 176 | |
177 | 177 | # Make sure MIME attachments also works correctly with other encodings than utf-8 |
… |
… |
class MailTests(TestCase):
|
180 | 180 | msg = EmailMultiAlternatives('Subject', text_content, 'from@example.com', ['to@example.com']) |
181 | 181 | msg.encoding = 'iso-8859-1' |
182 | 182 | msg.attach_alternative(html_content, "text/html") |
183 | | self.assertEqual(msg.message().get_payload(0).as_string(), 'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.') |
184 | | self.assertEqual(msg.message().get_payload(1).as_string(), 'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>') |
| 183 | self.assertEqual(msg.message().get_payload(0).as_string(), 'MIME-Version: 1.0\nContent-Type: text/plain; charset="iso-8859-1"\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.') |
| 184 | self.assertEqual(msg.message().get_payload(1).as_string(), 'MIME-Version: 1.0\nContent-Type: text/html; charset="iso-8859-1"\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>') |
185 | 185 | |
186 | 186 | def test_attachments(self): |
187 | 187 | """Regression test for #9367""" |
… |
… |
class BaseEmailBackendTests(object):
|
441 | 441 | email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'], cc=['cc@example.com']) |
442 | 442 | mail.get_connection().send_messages([email]) |
443 | 443 | message = self.get_the_message() |
444 | | self.assertStartsWith(message.as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ') |
| 444 | self.assertStartsWith(message.as_string(), 'MIME-Version: 1.0\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ') |
445 | 445 | |
446 | 446 | def test_idn_send(self): |
447 | 447 | """ |
… |
… |
class ConsoleBackendTests(BaseEmailBackendTests, TestCase):
|
589 | 589 | s = StringIO() |
590 | 590 | connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s) |
591 | 591 | send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection) |
592 | | self.assertTrue(s.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nDate: ')) |
| 592 | self.assertTrue(s.getvalue().startswith('MIME-Version: 1.0\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nDate: ')) |
593 | 593 | |
594 | 594 | |
595 | 595 | class FakeSMTPServer(smtpd.SMTPServer, threading.Thread): |