From aa11b3ea503ba1e6247e1629aa13f2752f32e3a9 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Sat, 13 May 2006 17:18:42 +0000 Subject: [PATCH] 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 --- django/core/mail.py | 5 ++++- docs/email.txt | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/core/mail.py b/django/core/mail.py index 3baf191b5c..415cb6e8fc 100644 --- a/django/core/mail.py +++ b/django/core/mail.py @@ -2,6 +2,7 @@ from django.conf import settings from email.MIMEText import MIMEText +from email.Header import Header import smtplib class BadHeaderError(ValueError): @@ -12,6 +13,8 @@ class SafeMIMEText(MIMEText): "Forbids multi-line headers, to prevent header injection." if '\n' in val or '\r' in val: 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) 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: continue from_email = from_email or settings.DEFAULT_FROM_EMAIL - msg = SafeMIMEText(message) + msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET) msg['Subject'] = subject msg['From'] = from_email msg['To'] = ', '.join(recipient_list) diff --git a/docs/email.txt b/docs/email.txt index b38b855cb3..37a4f38a5e 100644 --- a/docs/email.txt +++ b/docs/email.txt @@ -19,6 +19,13 @@ In two lines:: send_mail('Subject here', 'Here is the message.', 'from@example.com', ['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() ===========