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

Exception Parse Data #45

Closed
GuiAndra opened this issue Nov 27, 2017 · 19 comments
Closed

Exception Parse Data #45

GuiAndra opened this issue Nov 27, 2017 · 19 comments
Assignees
Milestone

Comments

@GuiAndra
Copy link

GuiAndra commented Nov 27, 2017

Hello, I'm getting this exception

error-email-imap

I added a try in the parse header function in the message class to pass the error.

private function parseHeader() {
$header = imap_fetchheader($this->client->connection, $this->uid, $this->fetch_options);
if ($header) {
$header = imap_rfc822_parse_headers($header);
}

if (property_exists($header, 'subject')) {
    $this->subject = imap_utf8($header->subject);
}
if (property_exists($header, 'date')) {
    try{
        $this->date = Carbon::parse($header->date);
    }catch (\Exception $e){
    }
}
if (property_exists($header, 'from')) {
    $this->from = $this->parseAddresses($header->from);
}
if (property_exists($header, 'to')) {
    $this->to = $this->parseAddresses($header->to);
}
if (property_exists($header, 'cc')) {
    $this->cc = $this->parseAddresses($header->cc);
}
if (property_exists($header, 'bcc')) {
    $this->bcc = $this->parseAddresses($header->bcc);
}

if (property_exists($header, 'reply_to')) {
    $this->reply_to = $this->parseAddresses($header->reply_to);
}
if (property_exists($header, 'sender')) {
    $this->sender = $this->parseAddresses($header->sender);
}

if (property_exists($header, 'message_id')) {
    $this->message_id = str_replace(['<', '>'], '', $header->message_id);
}
if (property_exists($header, 'Msgno')) {
    $this->message_no = ($this->fetch_options == FT_UID) ? trim($header->Msgno) : imap_msgno($this->client->getConnection(), trim($header->Msgno));
}

}

@Webklex
Copy link
Owner

Webklex commented Nov 29, 2017

Hi @GuiAndra ,
thanks for the report. I think I would rather like to validate the given datestring and perhaps "fix it" if it seems to be broken. This way anyone can be sure to always have a valid Carbon object.

Please give me a few days or perhaps the upcomming weekend to come up with a fix :)

Best regards

@Webklex Webklex self-assigned this Nov 29, 2017
@GuiAndra
Copy link
Author

Sure @Webklex, i'm waiting for the fix.

thanks :)

@GuiAndra GuiAndra reopened this Nov 29, 2017
@Webklex
Copy link
Owner

Webklex commented Dec 3, 2017

Hi @GuiAndra,
please update to v 1.0.3.9 :)

@Webklex Webklex closed this as completed Dec 3, 2017
@GuiAndra
Copy link
Author

GuiAndra commented Dec 4, 2017

Thaanks @Webklex :)

@RpCoding
Copy link

RpCoding commented Mar 27, 2018

04 Jan 2018 10:12:47 UT is a timestamp I have in one of the emails I'm trying to read. This caused a crash because of Carbon::parse.

Edit: My dirty fix was to replace

try{
    $this->date = Carbon::parse($date);
}catch(\Exception $e){
    switch(true){
        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}\ \([A-Z]{2,3}\+[0-9]{1,2}\:[0-9]{1,2})\)+$/i', $date):
            $array = explode('(', $date);
            $array = array_reverse($array);
            $date = trim(array_pop($array));
            break;
    }
    $this->date = Carbon::parse($date);
}

with

try{
    $this->date = Carbon::parse($date);
}catch(\Exception $e){
    switch(true){
        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}\ \([A-Z]{2,3}\+[0-9]{1,2}\:[0-9]{1,2})\)+$/i', $date):
            $array = explode('(', $date);
            $array = array_reverse($array);
            $date = trim(array_pop($array));
            break;
     }

    if(strpos($date, "UT") !== FALSE && strpos($date, "UTC") === FALSE){
        $date = str_replace("UT", "UTC", $date);
    }

    $this->date = Carbon::parse($date);
}

in https://github.com/Webklex/laravel-imap/blob/master/src/IMAP/Message.php but as I said, quick and dirty.

@Webklex
Copy link
Owner

Webklex commented Mar 28, 2018

@RpCoding thanks for your report :) I just released a new version containing the new validation rule.

Ref.: v. 1.0.5.5

@Davronjuraev
Copy link

Hello, please help me, How can i reply to message from laravel application

@brandxtc
Copy link

Got a new invalid timestamp. :)
Failed to parse time string (22 Jun 18 03:56:36 PM
-05:00 (GMT -05:00)) at position 35 (-): Double timezone specification

Webklex added a commit that referenced this issue Jul 21, 2018
@tortato
Copy link

tortato commented Nov 8, 2018

I got a new error into datetime:
"DateTime::__construct(): Failed to parse time string (Thu, 8 Nov 2018 08:54:58 -0200 (-02)) at position 31 ((): Unexpected character" -- double espaces into Thu, and 8

Sugestion

switch (true) {
  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:
  $date = str_replace('  ', ' ', $date);
  $array = explode('(', $date);
  $array = array_reverse($array);
  $date = trim(array_pop($array));
  break;
  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:
  
  $date .= 'C';
  break;
}

@Webklex
Copy link
Owner

Webklex commented Nov 24, 2018

@tortato wow you've found a new way to write a date^^ Thanks for the report and a solution. This will be part of the next release :)

Best regards

@AronWebSolutionPVTLTD
Copy link

@Webklex
I got a new error
DateTime::__construct(): Failed to parse time string (Sat, 31 Aug 2013 20:08:23 +0580) at position 30 (0): Unexpected character

image

@Webklex Webklex pinned this issue Jan 8, 2019
@archond
Copy link

archond commented Feb 5, 2019

@Webklex
Fri, 1 Feb 2019 01:30:04 +0600 (+06)
string №346
Fix my exception
case preg_match('/([A-Z]{2,3},\s+[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:
work it

@anibalealvarezs
Copy link

anibalealvarezs commented Feb 7, 2019

'DateTime::__construct(): Failed to parse time string (Mon, 4 Feb 2019 04:03:49 -0300 (-03)) at position 32 ((): Unexpected character

Note: there are 2 spaces between "Mon," and "4"

Webklex added a commit that referenced this issue Mar 11, 2019
@Webklex
Copy link
Owner

Webklex commented Mar 11, 2019

Hi @tortato @VijayTahkur @archond @anibalealvarezs ,
please update to v. 1.3.0 :)

@dmitryplus
Copy link

Hi @Webklex
I caught an exception 'Invalid message date' for the date
Sun, 6 Apr 2008 21:24:33 UT

v 1.4.1

@Webklex Webklex reopened this Jun 30, 2019
@Webklex Webklex added this to the v. 1.4.2 milestone Jun 30, 2019
Webklex added a commit that referenced this issue Jun 30, 2019
@Webklex Webklex closed this as completed Jun 30, 2019
@Eldar89
Copy link

Eldar89 commented Sep 11, 2019

@Webklex
I got a new error
Warning: DateTime::modify(): Failed to parse time string (Wed, 11 Sep 2019 15:23:06 +0600 (+06)) at position 32 ((): Unexpected character

v.1.4.2

Webklex added a commit that referenced this issue Nov 29, 2019
@bsralph
Copy link

bsralph commented Apr 9, 2020

@Webklex
I got the following error, could you assist?

DateTime::__construct(): Failed to parse time string (14 Sep 2019 00:10:08 UT +0200) at position 21 (U): The timezone could not be found in the database

@danielkleach
Copy link

@Webklex

Getting the following error:
Failed to parse time string (Tue, 08 Nov 2022 18:47:20 +0000 14:03:33 +0000) at position 32 (1): Double time specification

We are on version 1.6.2. Is this particular date error handled in later versions?

@rxmgrudy
Copy link

rxmgrudy commented Feb 16, 2023

For anyone that comes across this in the future, you can switch over to php-imap with very minor changes to your code and you can pass in a fallback_date to deal with errors related to parsing time strings.

https://github.com/Webklex/php-imap/blob/master/src/Header.php#L736

$clientManager = new ClientManager(['options' => ['fallback_date' => Carbon::now()]]);

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

No branches or pull requests