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)