diff --git a/app/Console/Commands/ParseEml.php b/app/Console/Commands/ParseEml.php index 7ffd45aba..ee272c666 100644 --- a/app/Console/Commands/ParseEml.php +++ b/app/Console/Commands/ParseEml.php @@ -83,6 +83,8 @@ public function handle() $this->info($message->getHeader()->raw); $this->line('From: '); $this->info(json_encode($message->getFrom()[0] ?? [], JSON_UNESCAPED_UNICODE)); + $this->line('Reply-To: '); + $this->info(json_encode($message->getReplyTo()[0] ?? [], JSON_UNESCAPED_UNICODE)); $this->line('In-Reply-To: '); $this->info($message->getInReplyTo()); $this->line('References: '); diff --git a/overrides/webklex/php-imap/src/Header.php b/overrides/webklex/php-imap/src/Header.php index 87aa4ff5f..9a2cd3133 100644 --- a/overrides/webklex/php-imap/src/Header.php +++ b/overrides/webklex/php-imap/src/Header.php @@ -241,7 +241,10 @@ public function rfc822_parse_headers($raw_headers) { $raw_imap_headers = (array)\imap_rfc822_parse_headers($raw_headers); foreach ($raw_imap_headers as $key => $values) { $key = str_replace("-", "_", $key); - $imap_headers[$key] = $values; + $values = $this->sanitizeHeaderValue($values); + if (!is_array($values) || (is_array($values) && count($values))) { + $imap_headers[$key] = $values; + } } } $lines = explode("\r\n", preg_replace("/\r\n\s/", ' ', $raw_headers)); @@ -322,12 +325,37 @@ public function rfc822_parse_headers($raw_headers) { } break; } - $headers[$key] = $value; + $value = $this->sanitizeHeaderValue($value); + if (!is_array($value) || (is_array($value) && count($value))) { + $headers[$key] = $value; + } elseif (is_array($value) && !count($value) && isset($headers[$key])) { + unset($headers[$key]); + } } return (object)array_merge($headers, $imap_headers); } + // https://github.com/freescout-help-desk/freescout/issues/4158 + public function sanitizeHeaderValue($value) + { + if (is_array($value)) { + foreach ($value as $i => $v) { + if (is_object($v) + && isset($v->mailbox) + && ($v->mailbox == '>' || $v->mailbox == 'INVALID_ADDRESS') + && ((isset($v->host) && ($v->host == '.SYNTAX-ERROR.' || $v->host === null)) || !isset($v->host)) + ) { + echo 'unset: '.$v->mailbox; + + unset($value[$i]); + } + } + } + + return $value; + } + /** * Decode MIME header elements * @link https://php.net/manual/en/function.imap-mime-header-decode.php