PHP "Invalid Date: header (not RFC 2822)"

SpamAssasin complains that the date format is wrong.
0.4 INVALID_DATE Invalid Date: header (not RFC 2822)

Supplied date is:
Date: Sat, 30 Apr 2022 11:20:15 0200

as a result of mail PHP script code:
$this->_add_hdr(‘Date’, sprintf(date(DateTime::RFC2822)));
or
$this->_add_hdr(‘Date’, sprintf(date(“D, d M Y H:i:s O”)));

(it seems to display same format no matter which line i use)
Do you have an idea how that PHP code line should look like so the error does not appear?

It seems to be missing a + before the 0200. Which is weird, according to the spec it should be there.

Which PHP version are you running?

Hi, thx, it is old 5.6.40. How can i modify the mentioned PHP code to produce valid output?

This is (to me at least) an odd usage of sprintf, where you’re putting the actual text string you want “printed” as the format parameter to sprintf. It’s parsing that string, and a “+” (or “-” ) will be used as formatting info, not output. I would replace it with this:

sprintf("%s",date("D, d M Y H:i:s O"))
1 Like

I have tried that, line being:
$this->_add_hdr('Date', sprintf("%s",date("D, d M Y H:i:s O")));

Result:
Date: Sat, 30 Apr 2022 15:31:47 0200

and the warning mentioned in 1st post remains… I am using that sprintf because i am not a developer, so if you have better idea how the line should look like?

Do you even need sprintf?

date("D, d M Y H:i:s O") gives me

Sat, 30 Apr 2022 17:54:11 +0000

@Gandalf No you’re right, the sprintf seems completely superfluous.

Could you show the code where this is coming from? I.e. how you’re ending up again with a date missing the +/-

When you use

This will of course have no + sign. Where should it come from? If you want to use this you need to use

sprintf("%s",date("D, d M Y H:i:s+O")

But normally

sprintf("%s", date(DateTime::RFC2822))

Should work…

You could try the following

date("D, d M Y H:i:'s +O")

Though I guess that might break on newer versions of PHP.

The + sign is included in the O format, it is an offset from GMT (either + or -)

EDIT, though I admit I have not looked at the PHP5 spec. Are you saying it wasn’t in there in PHP5?

1 Like

Sorry, of course you are right

$this->_add_hdr(‘Date’, date(“D, d M Y H:i:s O”));
doesn’t work. I do not know if printf is needed. If you have idea on alternative, i will try it.

These 2 not worked.

I have used:
$this->_add_hdr(‘Date’, date(“D, d M Y H:i:'s +O”));
that not worked either, result: Date: Sat, 30 Apr 2022 21:24:'37 0200

Whole file is there. At line 120 is the line…

I can see no output in this code. So how do you know that the + is missing?