From bb8ae70677de9dacfb88d6cf37e547d7959e697f Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 18:10:52 +0100 Subject: [PATCH 1/9] Added mailbox support for 'X-Forwarded-To' commonly used to identify rerouted emails from mailbox filters --- src/InboundEmail.php | 8 ++++++++ src/Routing/Route.php | 4 ++++ src/Routing/Router.php | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/src/InboundEmail.php b/src/InboundEmail.php index 089bb02..299d481 100644 --- a/src/InboundEmail.php +++ b/src/InboundEmail.php @@ -106,6 +106,14 @@ public function to(): array return $this->convertAddressHeader($this->message()->getHeader('To')); } + /** + * @return AddressPart[] + */ + public function xForwardedTo(): array + { + return $this->convertAddressHeader($this->message()->getHeader('X-Forwarded-To')); + } + /** * @return AddressPart[] */ diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 33e4eca..9032fe2 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -20,6 +20,7 @@ class Route const FROM = 'from'; const TO = 'to'; + const X_FORWARDED_TO = 'x-forwarded-to'; const CC = 'cc'; const BCC = 'bcc'; const SUBJECT = 'subject'; @@ -87,6 +88,9 @@ protected function gatherMatchSubjectsFromMessage(InboundEmail $message) case self::TO: return $this->convertMessageAddresses($message->to()); break; + case self::X_FORWARDED_TO: + return $this->convertMessageAddresses($message->xForwardedTo()); + break; case self::CC: return $this->convertMessageAddresses($message->cc()); break; diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 390d839..0864a47 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -41,6 +41,11 @@ public function to(string $pattern, $action): Route return $this->addRoute(Route::TO, $pattern, $action); } + public function xForwardedTo(string $pattern, $action): Route + { + return $this->addRoute(Route::X_FORWARDED_TO, $pattern, $action); + } + public function cc(string $pattern, $action): Route { return $this->addRoute(Route::CC, $pattern, $action); From 432b2732ac9c3d6676db5200bf3039b1ceb7eed8 Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 21:42:37 +0100 Subject: [PATCH 2/9] fixje hopelijk --- src/InboundEmail.php | 23 ++++++++++++++++++++--- src/Routing/Route.php | 20 +++++++++++++++++--- src/Routing/Router.php | 4 ++-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/InboundEmail.php b/src/InboundEmail.php index 299d481..ab19b58 100644 --- a/src/InboundEmail.php +++ b/src/InboundEmail.php @@ -10,7 +10,11 @@ use Illuminate\Support\Facades\Mail; use Illuminate\Support\Str; use ZBateson\MailMimeParser\Header\AddressHeader; +use ZBateson\MailMimeParser\Header\GenericHeader; use ZBateson\MailMimeParser\Header\Part\AddressPart; +use ZBateson\MailMimeParser\Header\Part\LiteralPart; +use ZBateson\MailMimeParser\Header\Part\ReceivedDomainPart; +use ZBateson\MailMimeParser\Header\ReceivedHeader; use ZBateson\MailMimeParser\Message as MimeMessage; use ZBateson\MailMimeParser\Message\Part\MessagePart; @@ -107,11 +111,11 @@ public function to(): array } /** - * @return AddressPart[] + * @return ReceivedDomainPart[] */ - public function xForwardedTo(): array + public function received(): array { - return $this->convertAddressHeader($this->message()->getHeader('X-Forwarded-To')); + return $this->convertReceivedHeaders($this->message()->getAllHeadersByName('received')); } /** @@ -139,6 +143,19 @@ protected function convertAddressHeader($header): array return []; } + protected function convertReceivedHeaders($headers): array + { + $parts = []; + + foreach($headers as $header){ + if ($header instanceof ReceivedHeader) { + $parts[] = $header->getParts(); + } + } + + return $parts; + } + /** * @return MessagePart[] */ diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 9032fe2..aa4c6b9 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -11,6 +11,8 @@ use Illuminate\Support\Str; use ReflectionFunction; use ZBateson\MailMimeParser\Header\Part\AddressPart; +use ZBateson\MailMimeParser\Header\Part\LiteralPart; +use ZBateson\MailMimeParser\Header\Part\ReceivedDomainPart; class Route { @@ -20,7 +22,7 @@ class Route const FROM = 'from'; const TO = 'to'; - const X_FORWARDED_TO = 'x-forwarded-to'; + const RECEIVED = 'received'; const CC = 'cc'; const BCC = 'bcc'; const SUBJECT = 'subject'; @@ -88,8 +90,8 @@ protected function gatherMatchSubjectsFromMessage(InboundEmail $message) case self::TO: return $this->convertMessageAddresses($message->to()); break; - case self::X_FORWARDED_TO: - return $this->convertMessageAddresses($message->xForwardedTo()); + case self::RECEIVED: + return $this->convertReceivedValues($message->received()); break; case self::CC: return $this->convertMessageAddresses($message->cc()); @@ -115,6 +117,18 @@ protected function convertMessageAddresses($addresses): array })->toArray(); } + /** + * @param $receivedParts ReceivedDomainPart[] + * @return array + */ + protected function convertReceivedValues($receivedParts): array + { + return Collection::make($receivedParts) + ->map(function (ReceivedDomainPart $receivedDomainPart) { + return $receivedDomainPart->getValue(); + })->toArray(); + } + public function run(InboundEmail $email) { $this->container = $this->container ?: new Container; diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 0864a47..cb3ad0c 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -41,9 +41,9 @@ public function to(string $pattern, $action): Route return $this->addRoute(Route::TO, $pattern, $action); } - public function xForwardedTo(string $pattern, $action): Route + public function received(string $pattern, $action): Route { - return $this->addRoute(Route::X_FORWARDED_TO, $pattern, $action); + return $this->addRoute(Route::RECEIVED, $pattern, $action); } public function cc(string $pattern, $action): Route From a5f03482fcab3bf30ace54e94fd02d3437e44a33 Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 22:12:34 +0100 Subject: [PATCH 3/9] Update Route.php --- src/Routing/Route.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Routing/Route.php b/src/Routing/Route.php index aa4c6b9..8163ab8 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -125,7 +125,7 @@ protected function convertReceivedValues($receivedParts): array { return Collection::make($receivedParts) ->map(function (ReceivedDomainPart $receivedDomainPart) { - return $receivedDomainPart->getValue(); + return str_replace('>','', str_replace('<','',$receivedDomainPart->getValue())); })->toArray(); } From 8de7aec94249b5658e7a0b4bf3b9131ccc76eaab Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 22:28:10 +0100 Subject: [PATCH 4/9] ReceivedPart only --- src/InboundEmail.php | 3 --- src/Routing/Route.php | 9 +++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/InboundEmail.php b/src/InboundEmail.php index ab19b58..3afc246 100644 --- a/src/InboundEmail.php +++ b/src/InboundEmail.php @@ -110,9 +110,6 @@ public function to(): array return $this->convertAddressHeader($this->message()->getHeader('To')); } - /** - * @return ReceivedDomainPart[] - */ public function received(): array { return $this->convertReceivedHeaders($this->message()->getAllHeadersByName('received')); diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 8163ab8..5e82f6f 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -13,6 +13,7 @@ use ZBateson\MailMimeParser\Header\Part\AddressPart; use ZBateson\MailMimeParser\Header\Part\LiteralPart; use ZBateson\MailMimeParser\Header\Part\ReceivedDomainPart; +use ZBateson\MailMimeParser\Header\Part\ReceivedPart; class Route { @@ -117,15 +118,11 @@ protected function convertMessageAddresses($addresses): array })->toArray(); } - /** - * @param $receivedParts ReceivedDomainPart[] - * @return array - */ protected function convertReceivedValues($receivedParts): array { return Collection::make($receivedParts) - ->map(function (ReceivedDomainPart $receivedDomainPart) { - return str_replace('>','', str_replace('<','',$receivedDomainPart->getValue())); + ->map(function (ReceivedPart $ReceivedPart) { + return str_replace('>','', str_replace('<','',$ReceivedPart->getValue())); })->toArray(); } From 8709bc884afe4ba97bb412d83cb20acc33384975 Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 22:44:22 +0100 Subject: [PATCH 5/9] fix --- src/InboundEmail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InboundEmail.php b/src/InboundEmail.php index 3afc246..525eff8 100644 --- a/src/InboundEmail.php +++ b/src/InboundEmail.php @@ -146,7 +146,7 @@ protected function convertReceivedHeaders($headers): array foreach($headers as $header){ if ($header instanceof ReceivedHeader) { - $parts[] = $header->getParts(); + $parts = $parts + $header->getParts(); } } From 794b0e971295cc6c6d5928cdd75a01959b532472 Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 22:49:58 +0100 Subject: [PATCH 6/9] Update InboundEmail.php --- src/InboundEmail.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/InboundEmail.php b/src/InboundEmail.php index 525eff8..bf9df9b 100644 --- a/src/InboundEmail.php +++ b/src/InboundEmail.php @@ -145,9 +145,9 @@ protected function convertReceivedHeaders($headers): array $parts = []; foreach($headers as $header){ - if ($header instanceof ReceivedHeader) { + $parts = $parts + $header->getParts(); - } + } return $parts; From 308c7a4820e92cc7efe0d71ecadf7cf5c598551f Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 22:55:29 +0100 Subject: [PATCH 7/9] Update Route.php --- src/Routing/Route.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 5e82f6f..1ec2521 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -121,7 +121,7 @@ protected function convertMessageAddresses($addresses): array protected function convertReceivedValues($receivedParts): array { return Collection::make($receivedParts) - ->map(function (ReceivedPart $ReceivedPart) { + ->map(function ($ReceivedPart) { return str_replace('>','', str_replace('<','',$ReceivedPart->getValue())); })->toArray(); } From 7b7db0e7717d29c7596d6a7ebeb3109b609a9580 Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Fri, 22 Mar 2024 23:54:43 +0100 Subject: [PATCH 8/9] Added mailbox support for 'X-Forwarded-To' commonly used to identify rerouted emails from mailbox filters Update Route.php Update InboundEmail.php fix ReceivedPart only Update Route.php fixje hopelijk --- src/InboundEmail.php | 22 ++++++++++++++++++++++ src/Routing/Route.php | 15 +++++++++++++++ src/Routing/Router.php | 5 +++++ 3 files changed, 42 insertions(+) diff --git a/src/InboundEmail.php b/src/InboundEmail.php index 089bb02..bf9df9b 100644 --- a/src/InboundEmail.php +++ b/src/InboundEmail.php @@ -10,7 +10,11 @@ use Illuminate\Support\Facades\Mail; use Illuminate\Support\Str; use ZBateson\MailMimeParser\Header\AddressHeader; +use ZBateson\MailMimeParser\Header\GenericHeader; use ZBateson\MailMimeParser\Header\Part\AddressPart; +use ZBateson\MailMimeParser\Header\Part\LiteralPart; +use ZBateson\MailMimeParser\Header\Part\ReceivedDomainPart; +use ZBateson\MailMimeParser\Header\ReceivedHeader; use ZBateson\MailMimeParser\Message as MimeMessage; use ZBateson\MailMimeParser\Message\Part\MessagePart; @@ -106,6 +110,11 @@ public function to(): array return $this->convertAddressHeader($this->message()->getHeader('To')); } + public function received(): array + { + return $this->convertReceivedHeaders($this->message()->getAllHeadersByName('received')); + } + /** * @return AddressPart[] */ @@ -131,6 +140,19 @@ protected function convertAddressHeader($header): array return []; } + protected function convertReceivedHeaders($headers): array + { + $parts = []; + + foreach($headers as $header){ + + $parts = $parts + $header->getParts(); + + } + + return $parts; + } + /** * @return MessagePart[] */ diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 33e4eca..1ec2521 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -11,6 +11,9 @@ use Illuminate\Support\Str; use ReflectionFunction; use ZBateson\MailMimeParser\Header\Part\AddressPart; +use ZBateson\MailMimeParser\Header\Part\LiteralPart; +use ZBateson\MailMimeParser\Header\Part\ReceivedDomainPart; +use ZBateson\MailMimeParser\Header\Part\ReceivedPart; class Route { @@ -20,6 +23,7 @@ class Route const FROM = 'from'; const TO = 'to'; + const RECEIVED = 'received'; const CC = 'cc'; const BCC = 'bcc'; const SUBJECT = 'subject'; @@ -87,6 +91,9 @@ protected function gatherMatchSubjectsFromMessage(InboundEmail $message) case self::TO: return $this->convertMessageAddresses($message->to()); break; + case self::RECEIVED: + return $this->convertReceivedValues($message->received()); + break; case self::CC: return $this->convertMessageAddresses($message->cc()); break; @@ -111,6 +118,14 @@ protected function convertMessageAddresses($addresses): array })->toArray(); } + protected function convertReceivedValues($receivedParts): array + { + return Collection::make($receivedParts) + ->map(function ($ReceivedPart) { + return str_replace('>','', str_replace('<','',$ReceivedPart->getValue())); + })->toArray(); + } + public function run(InboundEmail $email) { $this->container = $this->container ?: new Container; diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 390d839..cb3ad0c 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -41,6 +41,11 @@ public function to(string $pattern, $action): Route return $this->addRoute(Route::TO, $pattern, $action); } + public function received(string $pattern, $action): Route + { + return $this->addRoute(Route::RECEIVED, $pattern, $action); + } + public function cc(string $pattern, $action): Route { return $this->addRoute(Route::CC, $pattern, $action); From 1328cd8c2016c492de7fe89936ec0728b6c3c290 Mon Sep 17 00:00:00 2001 From: Maxime Hinnekens Date: Sat, 23 Mar 2024 00:25:50 +0100 Subject: [PATCH 9/9] Update InboundEmail.php --- src/InboundEmail.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/InboundEmail.php b/src/InboundEmail.php index bf9df9b..0df5b06 100644 --- a/src/InboundEmail.php +++ b/src/InboundEmail.php @@ -145,9 +145,7 @@ protected function convertReceivedHeaders($headers): array $parts = []; foreach($headers as $header){ - - $parts = $parts + $header->getParts(); - + $parts = array_merge($header->getParts(), $parts); } return $parts;