Opened 11 years ago

Closed 11 years ago

#3554 closed (fixed)

[patch] implement explicit timezone offset in date header for emails generated by django

Reported by: mrmachine <real dot human at mrmachine dot net> Owned by: Adrian Holovaty
Component: Core (Mail) Version: master
Severity: Keywords: mail timezone rfc822 rfc2822
Cc: real.human@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


django.core.mail uses formatdate() from the rfc822 module which has been deprecated as of python 2.3 in favour of the email module (, which contains an equivelent Utils.formatdate() method.

the problem with rfc822.formatdate() is that it includes the timezone offset as a named string which some popular MUA's misread or ignore (such as apple's, causing emails generated by django to appear out of order as the date header is assumed to be local time when it is actually GMT.

>>> import rfc822       
>>> from email import Utils
>>> rfc822.formatdate()
'Fri, 23 Feb 2007 00:16:54 GMT'
>>> Utils.formatdate()
'Fri, 23 Feb 2007 00:17:01 -0000'

RFC822 was made obsolete by RFC2822 ( which states:

The zone specifies the offset from Coordinated Universal Time (UTC,
formerly referred to as "Greenwich Mean Time") that the date and
time-of-day represent.  The "+" or "-" indicates whether the
time-of-day is ahead of (i.e., east of) or behind (i.e., west of)
Universal Time.  The first two digits indicate the number of hours
difference from Universal Time, and the last two digits indicate the
number of minutes difference from Universal Time.  (Hence, +hhmm
means +(hh * 60 + mm) minutes, and -hhmm means -(hh * 60 + mm)
minutes).  The form "+0000" SHOULD be used to indicate a time zone at
Universal Time.  Though "-0000" also indicates Universal Time, it is
used to indicate that the time was generated on a system that may be
in a local time zone other than Universal Time and therefore
indicates that the date-time contains no information about the local
time zone.

email.Utils.formatdate() always returns the current Universal Time regardless of local server time with "-0000" as the timezone, which is correct according to the RFC above. attached is a patch which replaces rfc822.formatdate() with email.Utils.formatdate() in django.core.mail. it's a simple change which shouldn't break anything, but should improve compatibility with MUA's which don't understand or choose to ignore named timezones and expect an explicit offset instead.

Attachments (1)

explicit_timezone_offset.patch (774 bytes) - added by mrmachine <real dot human at mrmachine dot net> 11 years ago.
replace rfc822.formatdate() with email.Utils.formatdate() in django.core.mail

Download all attachments as: .zip

Change History (3)

Changed 11 years ago by mrmachine <real dot human at mrmachine dot net>

replace rfc822.formatdate() with email.Utils.formatdate() in django.core.mail

comment:1 Changed 11 years ago by Chris Beaven

Triage Stage: UnreviewedReady for checkin

looks good.

comment:2 Changed 11 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [4570]) Fixed #3554: django.core.mail now generates dates with the email module instead of the outdated rfc822 module. Thanks, real.human@…

Note: See TracTickets for help on using tickets.
Back to Top