From 955c3749850583159e3a6cd46547e984f06ad159 Mon Sep 17 00:00:00 2001 From: Jeroen van den Heuvel Date: Mon, 3 Feb 2020 17:09:53 +0100 Subject: [PATCH] Allow to have open & link tracking by SMTP headers SMTP header "X-PM-TrackOpens" and "X-PM-TrackLinks" will mapped to the correct payload name. This allows open and link tracking per swift message. --- src/Postmark/Transport.php | 24 +++++++++++++++++------- tests/TransportTest.php | 4 ++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Postmark/Transport.php b/src/Postmark/Transport.php index 34be046..29ea4d9 100644 --- a/src/Postmark/Transport.php +++ b/src/Postmark/Transport.php @@ -268,14 +268,24 @@ private function processHeaders(&$payload, $message) { if ($value instanceof \Swift_Mime_Headers_UnstructuredHeader || $value instanceof \Swift_Mime_Headers_OpenDKIMHeader) { - if($fieldName != 'X-PM-Tag'){ - array_push($headers, [ - "Name" => $fieldName, - "Value" => $value->getValue(), - ]); - }else{ - $payload["Tag"] = $value->getValue(); + + $reservedFields = [ + 'X-PM-Tag' => 'Tag', + 'X-PM-TrackOpens' => 'TrackOpens', + 'X-PM-TrackLinks' => 'TrackLinks', + ]; + + foreach ($reservedFields as $headerName => $payloadName) { + if($fieldName == $headerName){ + $payload[$payloadName] = $value->getValue(); + continue 2; + } } + + array_push($headers, [ + "Name" => $fieldName, + "Value" => $value->getValue(), + ]); } else if ($value instanceof \Swift_Mime_Headers_DateHeader || $value instanceof \Swift_Mime_Headers_IdentificationHeader || $value instanceof \Swift_Mime_Headers_ParameterizedHeader || diff --git a/tests/TransportTest.php b/tests/TransportTest.php index 4fe3218..6c13545 100644 --- a/tests/TransportTest.php +++ b/tests/TransportTest.php @@ -34,6 +34,8 @@ public function testSend() { $headers = $message->getHeaders(); $headers->addTextHeader('X-PM-Tag', 'movie-quotes'); + $headers->addTextHeader('X-PM-TrackOpens', false); + $headers->addTextHeader('X-PM-TrackLinks', 'None'); $transport = new PostmarkTransportStub([new Response(200)]); @@ -61,6 +63,8 @@ protected function assertExpectedMessageRequest($message, $request) 'Tag' => 'movie-quotes', 'TextBody' => 'Doo-wah-ditty.', 'HtmlBody' => 'Help me Rhonda', + 'TrackOpens' => $message->getHeaders()->get('X-PM-TrackOpens')->getValue(), + 'TrackLinks' => $message->getHeaders()->get('X-PM-TrackLinks')->getValue(), 'Headers' => [ ['Name' => 'Message-ID', 'Value' => '<' . $message->getHeaders()->get('Message-ID')->getId() . '>'], ['Name' => 'X-PM-KeepID', 'Value' => 'true'],