Ticket #12791: emailmessage-body-encoding.2.diff

File emailmessage-body-encoding.2.diff, 3.7 KB (added by oyvind, 14 years ago)

Also works with MIME attachments

  • 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
     
    207207        """
    208208        basetype, subtype = mimetype.split('/', 1)
    209209        if basetype == 'text':
     210            encoding = self.encoding or settings.DEFAULT_CHARSET
    210211            attachment = SafeMIMEText(smart_str(content,
    211                 settings.DEFAULT_CHARSET), subtype, settings.DEFAULT_CHARSET)
     212                encoding), subtype, encoding)
    212213        else:
    213214            # Encode non-text attachments with base64.
    214215            attachment = MIMEBase(basetype, subtype)
  • 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# 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'
     120
     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
     126# Make sure MIME attachments also works correctly with other encodings than utf-8
     127>>> text_content = 'Firstname Sürname is a great guy.'
     128>>> html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>'
     129>>> msg = EmailMultiAlternatives('Subject', text_content, 'from@example.com', ['to@example.com'])
     130>>> msg.encoding = 'iso-8859-1'
     131>>> msg.attach_alternative(html_content, "text/html")
     132>>> msg.message().as_string()
     133'Content-Type: multipart/alternative; boundary="===============...=="\nMIME-Version: 1.0\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nDate: ...\nMessage-ID: <...>\n\n--===============...==\nContent-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.\n--===============...==\nContent-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>\n--===============...==--'
     134
    115135# Handle attachments within an multipart/alternative mail correctly (#9367)
    116136# (test is not as precise/clear as it could be w.r.t. email tree structure,
    117137#  but it's good enough.)
    118 
    119138>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
    120139>>> subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
    121140>>> text_content = 'This is an important message.'
Back to Top