Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webklex\PHPIMAP\Exceptions\GetMessagesFailedException Invalid message date. #173

Closed
dannylifino opened this issue Oct 19, 2021 · 10 comments
Closed

Comments

@dannylifino
Copy link

So currently I am having an issue with fetching emails from a mailbox due to an invalid message date of 1 mail. Although this is only 1 mail, this stops the entire mailbox from read out and I can't seem to find a solution for the issue.

The code, which seems like basic throws error: Webklex\PHPIMAP\Exceptions\GetMessagesFailedException Invalid message date. I have not changed the config regarding the date formats either.

$inboxMessages = $this->inboxFolder->messages()->all()->get(); $spamMessages = $this->spamFolder->messages()->all()->get();

Is there a way to get the fetch working with filtering out the invalid one?

@Webklex
Copy link
Owner

Webklex commented Oct 19, 2021

Hi @dannylifino ,
many thanks for your report.

Its likely caused by this method:

php-imap/src/Header.php

Lines 685 to 742 in e22be3e

/**
* Exception handling for invalid dates
*
* Currently known invalid formats:
* ^ Datetime ^ Problem ^ Cause
* | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature
* | Thu, 8 Nov 2018 08:54:58 -0200 (-02) |
* | | and invalid timezone (max 6 char) |
* | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown
* | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
* | | mail server |
* | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/
*
* Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues)
*
* @param object $header
*
* @throws InvalidMessageDateException
*/
private function parseDate($header) {
if (property_exists($header, 'date')) {
$parsed_date = null;
$date = $header->date;
if(preg_match('/\+0580/', $date)) {
$date = str_replace('+0580', '+0530', $date);
}
$date = trim(rtrim($date));
try {
$parsed_date = Carbon::parse($date);
} catch (\Exception $e) {
switch (true) {
case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0:
$date .= 'C';
break;
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ \+[0-9]{2,4}\ \(\+[0-9]{1,2}\))+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}[\,|\ \,]\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}.*)+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}\, \ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0:
case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{2,4}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}\ [A-Z]{2}\ \-[0-9]{2}\:[0-9]{2}\ \([A-Z]{2,3}\ \-[0-9]{2}:[0-9]{2}\))+$/i', $date) > 0:
$array = explode('(', $date);
$array = array_reverse($array);
$date = trim(array_pop($array));
break;
}
try{
$parsed_date = Carbon::parse($date);
} catch (\Exception $_e) {
throw new InvalidMessageDateException("Invalid message date. ID:".$this->get("message_id"), 1100, $e);
}
}
$this->set("date", $parsed_date);
}
}

As you can see here:

php-imap/src/Header.php

Lines 719 to 727 in e22be3e

case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0:
$date .= 'C';
break;
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ \+[0-9]{2,4}\ \(\+[0-9]{1,2}\))+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}[\,|\ \,]\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}.*)+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0:
case preg_match('/([A-Z]{2,3}\, \ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0:
case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{2,4}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}\ [A-Z]{2}\ \-[0-9]{2}\:[0-9]{2}\ \([A-Z]{2,3}\ \-[0-9]{2}:[0-9]{2}\))+$/i', $date) > 0:

It tries to detect and fix broken dates. A history of broken date formats can be found here: Webklex/laravel-imap#45

Anyway, in order to solve the problem two things are required:

  • The broken date (open at the header of the broken message and look for a line which starts with Date (e.g.: Date: Tue, 19 Oct 2021 05:12:41 -0700))
  • A regex to detect the date format

Best regards,

@dannylifino
Copy link
Author

@Webklex
Thanks for your response!

I am really struggling to this part, because there are no code samples availabe on how I should go about this approuch. Currently there are about 500 messages send daily to the mailbox, and it gives back as ID: ' ID:[email protected]', which I can't use to look up the message.

I would really appreciate your help!

@Webklex
Copy link
Owner

Webklex commented Oct 25, 2021

Can you post the entire error stack? It might contain additional information (such as the date in question).
Which part of a message do you need in order to find it?

You could also dump $headers in https://github.com/Webklex/php-imap/blob/master/src/Query/Query.php#L222 and go from there :)

Best regards,

@dannylifino
Copy link
Author

dannylifino commented Oct 26, 2021

@Webklex Thanks for your quick response once again :) This is the error log that I get:

Invalid message date. ID:[email protected]

at C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:347
343▕ return $this->populate($available_messages);
344▕ }
345▕ return MessageCollection::make([]);
346▕ } catch (Exception $e) {
➜ 347▕ throw new GetMessagesFailedException($e->getMessage(), 0, $e);
348▕ }
349▕ }
350▕
351▕ /**

1 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:766
Webklex\PHPIMAP\Exceptions\GetMessagesFailedException::("Invalid message date. ID:[email protected]")

2 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:265
Webklex\PHPIMAP\Query\Query::handleException()

@Webklex
Copy link
Owner

Webklex commented Oct 28, 2021

How unfortunate..

That message seems to be partially fcked up. Please dump the contents of $headers in

For example:

var_dump($headers);
die("1");

Or $header in

That should provide you all required information to identify the faulty message.

Best regards,

Webklex added a commit that referenced this issue Oct 28, 2021
@dannylifino
Copy link
Author

Thanks!

@dannylifino
Copy link
Author

@Webklex Is it also possible to solve this issue without editing the composer package?

@Webklex
Copy link
Owner

Webklex commented Nov 4, 2021

Hi @dannylifino ,
yes there is, if you use the current master. As you can see in 8cf2367 the exception will now contain the troubling date :)

Best regards,

@Webklex
Copy link
Owner

Webklex commented Nov 4, 2021

Hi @dannylifino ,
please give v3.0.0-alpha a try :)

Best regards,

@Webklex Webklex closed this as completed Nov 20, 2021
@mbol8309
Copy link

I'm having the same problem, debugging find out that date with format "2022.07.27-14.35.59" its not parseable. can you fix that @Webklex . thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants