WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 2 years ago

#40390 new defect (bug)

Wrong time (and date) in mail header send by phpmailer

Reported by: Drivingralle Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.3
Component: Mail Keywords:
Focuses: Cc:
PR Number:

Description

I noticed that some mail clients show wrong time and sometimes date (around midnight) while forwarding or otherwise dealing with emails. For ex. MS Outlook gets it wrong.
The offset is the same as my local timeoffset to UTC.

Tracking it down this is because the rfcDate() within wp-includes/class-phpmailer.php uses date_default_timezone_get() to add the date and time into the mail header. This value is forced by WP to be UTC.

Debugging rfcDate() with:

error_log( var_export( date_default_timezone_get(), true ) );
error_log( var_export( date('D, j M Y H:i:s O'), true ) );
error_log( var_export( current_time( 'D, j M Y H:i:s O' ), true ) );

I get:

[07-Apr-2017 12:01:02 UTC] 'UTC'
[07-Apr-2017 12:01:02 UTC] 'Fri, 7 Apr 2017 12:01:02 +0000'
[07-Apr-2017 12:01:02 UTC] 'Fri, 7 Apr 2017 14:01:02 +0000'

I read this ticket #39595 and I guess it will not be changed.
The ticket also says there is no WP function using the native timezone getter. This shows there is. Or at least a lib within core.

Can we modify the phpmailer class to use current_time() or do we need to set and reset the timezone before and after every phpmailer call?

Change History (1)

#1 @Rarst
2 years ago

Note that current_time() itself calls date() and assumes default timezone. In other words it will ignore WP time zone and output completely invalid stuff unless gmt arg is set to true.

<?php
var_dump( date_default_timezone_get() );
// "UTC"

var_dump( date('D, j M Y H:i:s O') );
// "Tue, 2 May 2017 11:38:08 +0000" << correct UTC

var_dump( current_time( 'D, j M Y H:i:s O' ) );
// "Tue, 2 May 2017 14:38:08 +0000" << local time, invalid time zone

var_dump( current_time( 'D, j M Y H:i:s O', true ) );
// "Tue, 2 May 2017 11:38:08 +0000" << correct UTC

So your last suggestion to use current_time() will just increase breakage. :(

I am not sure why would times be "wrong" without more context. If date() without input is used they would be correct, just always in UTC (which consuming email clients should be able to display in desired time zone I would assume).

The more likely issue is that WP API is feeding broken input into mailer class.

PS chimed in on current_time() issue in https://core.trac.wordpress.org/ticket/38940#comment:8

Last edited 2 years ago by Rarst (previous) (diff)
Note: See TracTickets for help on using tickets.