Skip to content

Commit 14376cf

Browse files
committed
Fix for #45 DateTime::__construct(): Failed to parse time string (...) provided
1 parent 102519a commit 14376cf

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/IMAP/Message.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,31 @@ private function parseHeader() {
231231
$this->subject = imap_utf8($header->subject);
232232
}
233233
if (property_exists($header, 'date')) {
234-
$this->date = Carbon::parse($header->date);
234+
$date = $header->date;
235+
236+
/**
237+
* Exception handling for invalid dates
238+
* Will be extended in the future
239+
*
240+
* Currently known invalid formats:
241+
* ^ Datetime ^ Problem ^ Cause ^
242+
* | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
243+
* | | and invalid timezone (max 6 char) | |
244+
*
245+
* Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45)
246+
*/
247+
try{
248+
$this->date = Carbon::parse($date);
249+
}catch(\Exception $e){
250+
switch(true){
251+
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):
252+
$array = explode('(', $date);
253+
$array = array_reverse($array);
254+
$date = trim(array_pop($array));
255+
break;
256+
}
257+
$this->date = Carbon::parse($date);
258+
}
235259
}
236260

237261
if (property_exists($header, 'from')) {

0 commit comments

Comments
 (0)