Ticket #13829: email_charset.patch

File email_charset.patch, 2.8 KB (added by Ian Lewis, 14 years ago)

Patch for the EMAIL_CHARSET setting.

  • django/conf/global_settings.py

    diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
    index cdff2ff..4ac44b5 100644
    a b EMAIL_HOST = 'localhost'  
    165165# Port for sending e-mail.
    166166EMAIL_PORT = 25
    167167
     168# Default character set for email. Defaults to DEFAULT_CHARSET
     169EMAIL_CHARSET = None
     170
    168171# Optional SMTP authentication information for EMAIL_HOST.
    169172EMAIL_HOST_USER = ''
    170173EMAIL_HOST_PASSWORD = ''
  • django/core/mail/message.py

    diff --git a/django/core/mail/message.py b/django/core/mail/message.py
    index 91a10a0..297b8e9 100644
    a b def make_msgid(idstring=None):  
    5656
    5757def forbid_multi_line_headers(name, val, encoding):
    5858    """Forbids multi-line headers, to prevent header injection."""
    59     encoding = encoding or settings.DEFAULT_CHARSET
     59    encoding = encoding or settings.EMAIL_CHARSET or settings.DEFAULT_CHARSET
    6060    val = force_unicode(val)
    6161    if '\n' in val or '\r' in val:
    6262        raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name))
    class EmailMessage(object):  
    138138        return self.connection
    139139
    140140    def message(self):
    141         encoding = self.encoding or settings.DEFAULT_CHARSET
     141        encoding = self.encoding or settings.EMAIL_CHARSET or settings.DEFAULT_CHARSET
    142142        msg = SafeMIMEText(smart_str(self.body, encoding),
    143143                           self.content_subtype, encoding)
    144144        msg = self._create_message(msg)
  • tests/regressiontests/mail/tests.py

    diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
    index 84be585..6027434 100644
    a b BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection T  
    131131>>> email.message()['Subject'].encode()
    132132u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?='
    133133
     134# Regression for #XXXXX - Make sure the EMAIL_CHARSET is used.
     135>>> old_email_charset = settings.EMAIL_CHARSET
     136>>> settings.EMAIL_CHARSET = 'iso-2022-jp'
     137>>> email = EmailMessage('件名', '本文', 'bounce@example.com', ['"仲居良介" <to@example.com>'], headers={'From': '"池田洋介" <from@example.com>'})
     138>>> message = email.message()
     139>>> message['From']
     140'=?iso-2022-jp?b?GyRCQ1NFRE1OMnAbKEI=?= <from@example.com>'
     141>>> message['To']
     142'=?iso-2022-jp?b?GyRCQ2c1b05JMnAbKEI=?= <to@example.com>'
     143>>> message["Subject"].encode()
     144u'=?iso-2022-jp?b?GyRCN29MPhsoQg==?='
     145>>> settings.EMAIL_CHARSET = old_email_charset
     146
    134147# Make sure headers can be set with a different encoding than utf-8 in SafeMIMEMultipart as well
    135148>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
    136149>>> subject, from_email, to = 'hello', 'from@example.com', '"Sürname, Firstname" <to@example.com>'
Back to Top