From db5be42c124886eab758e5f4c181db32af174e23 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 1 Apr 2010 15:16:26 +0000 Subject: [PATCH] Fixed #13259 -- Ensure that multiple calls to message() don't corrupt any extra message headers. Thanks to canburak for the report, and Andi Albrecht for the fix. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12901 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/mail/message.py | 4 +++- tests/regressiontests/mail/tests.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index a288d6c5fc..91a10a0a5c 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -143,7 +143,7 @@ class EmailMessage(object): self.content_subtype, encoding) msg = self._create_message(msg) msg['Subject'] = self.subject - msg['From'] = self.extra_headers.pop('From', self.from_email) + msg['From'] = self.extra_headers.get('From', self.from_email) msg['To'] = ', '.join(self.to) # Email header names are case-insensitive (RFC 2045), so we have to @@ -154,6 +154,8 @@ class EmailMessage(object): if 'message-id' not in header_names: msg['Message-ID'] = make_msgid() for name, value in self.extra_headers.items(): + if name.lower() == 'from': # From is already handled + continue msg[name] = value return msg diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index e27804c11d..84be585bfd 100644 --- a/tests/regressiontests/mail/tests.py +++ b/tests/regressiontests/mail/tests.py @@ -101,6 +101,16 @@ BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection T >>> message['From'] 'from@example.com' +# Regression for #13259 - Make sure that headers are not changed +# when calling EmailMessage.message() +>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> message = email.message() +>>> message['From'] +'from@example.com' +>>> message = email.message() +>>> message['From'] +'from@example.com' + # Regression for #11144 - When a to/from/cc header contains unicode, # make sure the email addresses are parsed correctly (especially # with regards to commas)