diff --git a/django/core/mail.py b/django/core/mail.py index 1ac2a39908..58588bf712 100644 --- a/django/core/mail.py +++ b/django/core/mail.py @@ -247,8 +247,14 @@ class EmailMessage(object): msg['Subject'] = self.subject msg['From'] = self.from_email msg['To'] = ', '.join(self.to) - msg['Date'] = formatdate() - msg['Message-ID'] = make_msgid() + + # Email header names are case-insensitive (RFC 2045), so we have to + # accommodate that when doing comparisons. + header_names = [key.lower() for key in self.extra_headers] + if 'date' not in header_names: + msg['Date'] = formatdate() + if 'message-id' not in header_names: + msg['Message-ID'] = make_msgid() for name, value in self.extra_headers.items(): msg[name] = value return msg diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index c8b85f48c6..c2c084c4c6 100644 --- a/tests/regressiontests/mail/tests.py +++ b/tests/regressiontests/mail/tests.py @@ -52,4 +52,12 @@ BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection T >>> message.as_string() 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Long subject lines that get wrapped should use a space continuation\n character to get expected behaviour in Outlook and Thunderbird\nFrom: from@example.com\nTo: to@example.com\nDate: ...\nMessage-ID: <...>\n\nContent' +# Specifying dates or message-ids in the extra headers overrides the defaul +# values (#9233). + +>>> headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} +>>> email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers) +>>> email.message().as_string() +'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent' + """