Ticket #13829: email_charset_with_option.patch

File email_charset_with_option.patch, 9.4 KB (added by Ian Lewis, 14 years ago)

A version of the patch which adds an encoding option to send_mail().

  • 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/__init__.py

    diff --git a/django/core/mail/__init__.py b/django/core/mail/__init__.py
    index f9d1210..3eb35cf 100644
    a b def get_connection(backend=None, fail_silently=False, **kwds):  
    4343
    4444def send_mail(subject, message, from_email, recipient_list,
    4545              fail_silently=False, auth_user=None, auth_password=None,
    46               connection=None):
     46              connection=None, encoding=None):
    4747    """
    4848    Easy wrapper for sending a single message to a recipient list. All members
    4949    of the recipient list will see the other recipients in the 'To' field.
    def send_mail(subject, message, from_email, recipient_list,  
    5858                                    password=auth_password,
    5959                                    fail_silently=fail_silently)
    6060    return EmailMessage(subject, message, from_email, recipient_list,
    61                         connection=connection).send()
     61                        connection=connection, encoding=encoding).send()
    6262
    6363
    6464def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
    65                    auth_password=None, connection=None):
     65                   auth_password=None, connection=None, encoding=None):
    6666    """
    6767    Given a datatuple of (subject, message, from_email, recipient_list), sends
    6868    each message to each recipient list. Returns the number of e-mails sent.
    def send_mass_mail(datatuple, fail_silently=False, auth_user=None,  
    7878    connection = connection or get_connection(username=auth_user,
    7979                                    password=auth_password,
    8080                                    fail_silently=fail_silently)
    81     messages = [EmailMessage(subject, message, sender, recipient)
     81    messages = [EmailMessage(subject, message, sender, recipient, encoding=encoding)
    8282                for subject, message, sender, recipient in datatuple]
    8383    return connection.send_messages(messages)
    8484
    8585
    86 def mail_admins(subject, message, fail_silently=False, connection=None):
     86def mail_admins(subject, message, fail_silently=False, connection=None, encoding=None):
    8787    """Sends a message to the admins, as defined by the ADMINS setting."""
    8888    if not settings.ADMINS:
    8989        return
    9090    EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
    9191                 settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
    92                  connection=connection).send(fail_silently=fail_silently)
     92                 connection=connection, encoding=encoding).send(fail_silently=fail_silently)
    9393
    9494
    95 def mail_managers(subject, message, fail_silently=False, connection=None):
     95def mail_managers(subject, message, fail_silently=False, connection=None, encoding=None):
    9696    """Sends a message to the managers, as defined by the MANAGERS setting."""
    9797    if not settings.MANAGERS:
    9898        return
    9999    EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
    100100                 settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
    101                  connection=connection).send(fail_silently=fail_silently)
     101                 connection=connection, encoding=encoding).send(fail_silently=fail_silently)
    102102
    103103
    104104class SMTPConnection(_SMTPConnection):
  • django/core/mail/message.py

    diff --git a/django/core/mail/message.py b/django/core/mail/message.py
    index 91a10a0..36cfd50 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):  
    102102    """
    103103    content_subtype = 'plain'
    104104    mixed_subtype = 'mixed'
    105     encoding = None     # None => use settings default
    106105
    107106    def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
    108                  connection=None, attachments=None, headers=None):
     107                 connection=None, attachments=None, headers=None, encoding=None):
    109108        """
    110109        Initialize a single email message (which can be sent to multiple
    111110        recipients).
    class EmailMessage(object):  
    130129        self.attachments = attachments or []
    131130        self.extra_headers = headers or {}
    132131        self.connection = connection
     132        self.encoding = encoding     # None => use settings default
    133133
    134134    def get_connection(self, fail_silently=False):
    135135        from django.core.mail import get_connection
    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..4c01287 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>'
    Message-ID: ...  
    414427Content
    415428-------------------------------------------------------------------------------
    416429
     430# Ticket XXXX: Test encoding option
     431>>> connection = console.EmailBackend()
     432>>> send_mail('Message from Firstname Sürname', 'Content', 'from@example.com',
     433...           ['"Sürname, Firstname" <to@example.com>','other@example.com'],
     434...           connection=connection, encoding='iso-8859-1')
     435Content-Type: text/plain; charset="iso-8859-1"
     436MIME-Version: 1.0
     437Content-Transfer-Encoding: quoted-printable
     438Subject: =?iso-8859-1?q?Message_from_Firstname_S=FCrname?=
     439From: from@example.com
     440To: =?iso-8859-1?q?S=FCrname=2C_Firstname?= <to@example.com>, other@example.com
     441Date: ...
     442Message-ID: ...
     443
     444Content
     445-------------------------------------------------------------------------------
     4461
     447
     448>>> send_mass_mail([
     449...         ('Message from Firstname Sürname', 'Content1', 'from1@example.com', ['to1@example.com']),
     450...         ('ぶちょからのメッセージ', 'Content2', 'from2@example.com', ['to2@example.com'])
     451...     ], connection=connection, encoding='utf-8')
     452Content-Type: text/plain; charset="utf-8"
     453MIME-Version: 1.0
     454Content-Transfer-Encoding: quoted-printable
     455Subject: =?utf-8?q?Message_from_Firstname_S=C3=BCrname?=
     456From: from1@example.com
     457To: to1@example.com
     458Date: ...
     459Message-ID: ...
     460
     461Content1
     462-------------------------------------------------------------------------------
     463Content-Type: text/plain; charset="utf-8"
     464MIME-Version: 1.0
     465Content-Transfer-Encoding: quoted-printable
     466Subject: =?utf-8?b?44G244Gh44KH44GL44KJ44Gu44Oh44OD44K744O844K4?=
     467From: from2@example.com
     468To: to2@example.com
     469Date: ...
     470Message-ID: ...
     471
     472Content2
     473-------------------------------------------------------------------------------
     4742
     475
     476>>> mail_admins('件名', '本文', connection=connection, encoding='utf-8')
     477Content-Type: text/plain; charset="utf-8"
     478MIME-Version: 1.0
     479Content-Transfer-Encoding: quoted-printable
     480Subject: =?utf-8?b?W0RqYW5nb10g5Lu25ZCN?=
     481From: root@localhost
     482To: nobody@example.com
     483Date: ...
     484Message-ID: ...
     485
     486=E6=9C=AC=E6=96=87
     487-------------------------------------------------------------------------------
     488
     489>>> mail_managers('件名', '本文', connection=connection)
     490Content-Type: text/plain; charset="utf-8"
     491MIME-Version: 1.0
     492Content-Transfer-Encoding: quoted-printable
     493Subject: =?utf-8?b?W0RqYW5nb10g5Lu25ZCN?=
     494From: root@localhost
     495To: nobody@example.com
     496Date: ...
     497Message-ID: ...
     498
     499=E6=9C=AC=E6=96=87
     500-------------------------------------------------------------------------------
     501
    417502>>> settings.ADMINS = old_admins
    418503>>> settings.MANAGERS = old_managers
    419504
Back to Top