Fixed #1235: email sent from {{{django.core.mail}}} will now be encoded using
the value of the {{{DEFAULT_CHARSET}}} setting. Thanks, igor@goryachev.org and akaihola. git-svn-id: http://code.djangoproject.com/svn/django/trunk@2901 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
5c0e4f3908
commit
aa11b3ea50
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from email.MIMEText import MIMEText
|
from email.MIMEText import MIMEText
|
||||||
|
from email.Header import Header
|
||||||
import smtplib
|
import smtplib
|
||||||
|
|
||||||
class BadHeaderError(ValueError):
|
class BadHeaderError(ValueError):
|
||||||
|
@ -12,6 +13,8 @@ class SafeMIMEText(MIMEText):
|
||||||
"Forbids multi-line headers, to prevent header injection."
|
"Forbids multi-line headers, to prevent header injection."
|
||||||
if '\n' in val or '\r' in val:
|
if '\n' in val or '\r' in val:
|
||||||
raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
|
raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
|
||||||
|
if name == "Subject":
|
||||||
|
val = Header(val, settings.DEFAULT_CHARSET)
|
||||||
MIMEText.__setitem__(self, name, val)
|
MIMEText.__setitem__(self, name, val)
|
||||||
|
|
||||||
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
|
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
|
||||||
|
@ -42,7 +45,7 @@ def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST
|
||||||
if not recipient_list:
|
if not recipient_list:
|
||||||
continue
|
continue
|
||||||
from_email = from_email or settings.DEFAULT_FROM_EMAIL
|
from_email = from_email or settings.DEFAULT_FROM_EMAIL
|
||||||
msg = SafeMIMEText(message)
|
msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET)
|
||||||
msg['Subject'] = subject
|
msg['Subject'] = subject
|
||||||
msg['From'] = from_email
|
msg['From'] = from_email
|
||||||
msg['To'] = ', '.join(recipient_list)
|
msg['To'] = ', '.join(recipient_list)
|
||||||
|
|
|
@ -20,6 +20,13 @@ In two lines::
|
||||||
send_mail('Subject here', 'Here is the message.', 'from@example.com',
|
send_mail('Subject here', 'Here is the message.', 'from@example.com',
|
||||||
['to@example.com'], fail_silently=False)
|
['to@example.com'], fail_silently=False)
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
The character set of email sent with ``django.core.mail`` will be set to
|
||||||
|
the value of your `DEFAULT_CHARSET setting`_.
|
||||||
|
|
||||||
|
.. _DEFAULT_CHARSET setting: ../settings/#DEFAULT_CHARSET
|
||||||
|
|
||||||
send_mail()
|
send_mail()
|
||||||
===========
|
===========
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue