Skip to content

Commit

Permalink
Allow to have open & link tracking by SMTP headers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jeroen van den Heuvel committed Feb 3, 2020
1 parent f3932a6 commit 955c374
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/Postmark/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
4 changes: 4 additions & 0 deletions tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);

Expand Down Expand Up @@ -61,6 +63,8 @@ protected function assertExpectedMessageRequest($message, $request)
'Tag' => 'movie-quotes',
'TextBody' => 'Doo-wah-ditty.',
'HtmlBody' => '<q>Help me Rhonda</q>',
'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'],
Expand Down

0 comments on commit 955c374

Please sign in to comment.