Ticket #12791: emailmessage-body-encoding.diff

File emailmessage-body-encoding.diff, 2.2 KB (added by oyvind, 15 years ago)

Patch with tests

  • django/core/mail/message.py

     
    131131
    132132    def message(self):
    133133        encoding = self.encoding or settings.DEFAULT_CHARSET
    134         msg = SafeMIMEText(smart_str(self.body, settings.DEFAULT_CHARSET),
     134        msg = SafeMIMEText(smart_str(self.body, encoding),
    135135                           self.content_subtype, encoding)
    136136        msg = self._create_message(msg)
    137137        msg['Subject'] = self.subject
  • tests/regressiontests/mail/tests.py

     
    112112>>> email.message()['To']
    113113'=?utf-8?q?S=C3=BCrname=2C_Firstname?= <to@example.com>, other@example.com'
    114114
    115 # Handle attachments within an multipart/alternative mail correctly (#9367)
    116 # (test is not as precise/clear as it could be w.r.t. email tree structure,
    117 #  but it's good enough.)
     115# Regression for #12791  - Encode body correctly with other encodings than utf-8
     116>>> email = EmailMessage('Subject', 'Æ e trønder æ å hærregud kor tøff æ e', 'from@example.com', ['other@example.com'])
     117>>> message = email.message()
     118>>> message.as_string()
     119'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com\nDate: ...\nMessage-ID: <...>\n\n=C3=86 e tr=C3=B8nder =C3=A6 =C3=A5 h=C3=A6rregud kor t=C3=B8ff =C3=A6 e'
    118120
     121>>> email.encoding = 'iso-8859-1'
     122>>> message = email.message()
     123>>> message.as_string()
     124'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\nDate: ...\nMessage-ID: <...>\n\n=C6 e tr=F8nder =E6 =E5 h=E6rregud kor t=F8ff =E6 e'
     125
    119126>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
    120127>>> subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
    121128>>> text_content = 'This is an important message.'
Back to Top