Ticket #13829: email_charset_with_option.patch
File email_charset_with_option.patch, 9.4 KB (added by , 14 years ago) |
---|
-
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' 165 165 # Port for sending e-mail. 166 166 EMAIL_PORT = 25 167 167 168 # Default character set for email. Defaults to DEFAULT_CHARSET 169 EMAIL_CHARSET = None 170 168 171 # Optional SMTP authentication information for EMAIL_HOST. 169 172 EMAIL_HOST_USER = '' 170 173 EMAIL_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): 43 43 44 44 def send_mail(subject, message, from_email, recipient_list, 45 45 fail_silently=False, auth_user=None, auth_password=None, 46 connection=None ):46 connection=None, encoding=None): 47 47 """ 48 48 Easy wrapper for sending a single message to a recipient list. All members 49 49 of the recipient list will see the other recipients in the 'To' field. … … def send_mail(subject, message, from_email, recipient_list, 58 58 password=auth_password, 59 59 fail_silently=fail_silently) 60 60 return EmailMessage(subject, message, from_email, recipient_list, 61 connection=connection ).send()61 connection=connection, encoding=encoding).send() 62 62 63 63 64 64 def send_mass_mail(datatuple, fail_silently=False, auth_user=None, 65 auth_password=None, connection=None ):65 auth_password=None, connection=None, encoding=None): 66 66 """ 67 67 Given a datatuple of (subject, message, from_email, recipient_list), sends 68 68 each message to each recipient list. Returns the number of e-mails sent. … … def send_mass_mail(datatuple, fail_silently=False, auth_user=None, 78 78 connection = connection or get_connection(username=auth_user, 79 79 password=auth_password, 80 80 fail_silently=fail_silently) 81 messages = [EmailMessage(subject, message, sender, recipient )81 messages = [EmailMessage(subject, message, sender, recipient, encoding=encoding) 82 82 for subject, message, sender, recipient in datatuple] 83 83 return connection.send_messages(messages) 84 84 85 85 86 def mail_admins(subject, message, fail_silently=False, connection=None ):86 def mail_admins(subject, message, fail_silently=False, connection=None, encoding=None): 87 87 """Sends a message to the admins, as defined by the ADMINS setting.""" 88 88 if not settings.ADMINS: 89 89 return 90 90 EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message, 91 91 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) 93 93 94 94 95 def mail_managers(subject, message, fail_silently=False, connection=None ):95 def mail_managers(subject, message, fail_silently=False, connection=None, encoding=None): 96 96 """Sends a message to the managers, as defined by the MANAGERS setting.""" 97 97 if not settings.MANAGERS: 98 98 return 99 99 EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message, 100 100 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) 102 102 103 103 104 104 class 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): 56 56 57 57 def forbid_multi_line_headers(name, val, encoding): 58 58 """Forbids multi-line headers, to prevent header injection.""" 59 encoding = encoding or settings. DEFAULT_CHARSET59 encoding = encoding or settings.EMAIL_CHARSET or settings.DEFAULT_CHARSET 60 60 val = force_unicode(val) 61 61 if '\n' in val or '\r' in val: 62 62 raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name)) … … class EmailMessage(object): 102 102 """ 103 103 content_subtype = 'plain' 104 104 mixed_subtype = 'mixed' 105 encoding = None # None => use settings default106 105 107 106 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): 109 108 """ 110 109 Initialize a single email message (which can be sent to multiple 111 110 recipients). … … class EmailMessage(object): 130 129 self.attachments = attachments or [] 131 130 self.extra_headers = headers or {} 132 131 self.connection = connection 132 self.encoding = encoding # None => use settings default 133 133 134 134 def get_connection(self, fail_silently=False): 135 135 from django.core.mail import get_connection … … class EmailMessage(object): 138 138 return self.connection 139 139 140 140 def message(self): 141 encoding = self.encoding or settings. DEFAULT_CHARSET141 encoding = self.encoding or settings.EMAIL_CHARSET or settings.DEFAULT_CHARSET 142 142 msg = SafeMIMEText(smart_str(self.body, encoding), 143 143 self.content_subtype, encoding) 144 144 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 131 131 >>> email.message()['Subject'].encode() 132 132 u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?=' 133 133 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() 144 u'=?iso-2022-jp?b?GyRCN29MPhsoQg==?=' 145 >>> settings.EMAIL_CHARSET = old_email_charset 146 134 147 # Make sure headers can be set with a different encoding than utf-8 in SafeMIMEMultipart as well 135 148 >>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} 136 149 >>> subject, from_email, to = 'hello', 'from@example.com', '"Sürname, Firstname" <to@example.com>' … … Message-ID: ... 414 427 Content 415 428 ------------------------------------------------------------------------------- 416 429 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') 435 Content-Type: text/plain; charset="iso-8859-1" 436 MIME-Version: 1.0 437 Content-Transfer-Encoding: quoted-printable 438 Subject: =?iso-8859-1?q?Message_from_Firstname_S=FCrname?= 439 From: from@example.com 440 To: =?iso-8859-1?q?S=FCrname=2C_Firstname?= <to@example.com>, other@example.com 441 Date: ... 442 Message-ID: ... 443 444 Content 445 ------------------------------------------------------------------------------- 446 1 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') 452 Content-Type: text/plain; charset="utf-8" 453 MIME-Version: 1.0 454 Content-Transfer-Encoding: quoted-printable 455 Subject: =?utf-8?q?Message_from_Firstname_S=C3=BCrname?= 456 From: from1@example.com 457 To: to1@example.com 458 Date: ... 459 Message-ID: ... 460 461 Content1 462 ------------------------------------------------------------------------------- 463 Content-Type: text/plain; charset="utf-8" 464 MIME-Version: 1.0 465 Content-Transfer-Encoding: quoted-printable 466 Subject: =?utf-8?b?44G244Gh44KH44GL44KJ44Gu44Oh44OD44K744O844K4?= 467 From: from2@example.com 468 To: to2@example.com 469 Date: ... 470 Message-ID: ... 471 472 Content2 473 ------------------------------------------------------------------------------- 474 2 475 476 >>> mail_admins('件名', '本文', connection=connection, encoding='utf-8') 477 Content-Type: text/plain; charset="utf-8" 478 MIME-Version: 1.0 479 Content-Transfer-Encoding: quoted-printable 480 Subject: =?utf-8?b?W0RqYW5nb10g5Lu25ZCN?= 481 From: root@localhost 482 To: nobody@example.com 483 Date: ... 484 Message-ID: ... 485 486 =E6=9C=AC=E6=96=87 487 ------------------------------------------------------------------------------- 488 489 >>> mail_managers('件名', '本文', connection=connection) 490 Content-Type: text/plain; charset="utf-8" 491 MIME-Version: 1.0 492 Content-Transfer-Encoding: quoted-printable 493 Subject: =?utf-8?b?W0RqYW5nb10g5Lu25ZCN?= 494 From: root@localhost 495 To: nobody@example.com 496 Date: ... 497 Message-ID: ... 498 499 =E6=9C=AC=E6=96=87 500 ------------------------------------------------------------------------------- 501 417 502 >>> settings.ADMINS = old_admins 418 503 >>> settings.MANAGERS = old_managers 419 504