Skip to content

Commit

Permalink
Add Postmark Message ID to response headers (#49)
Browse files Browse the repository at this point in the history
Pulls in changes from #46 (thank you @dejury, and sorry for the wait! 🙇 ) and adds a test for it.

Closes #46 #24 (had to rebuild the PR after the repo move).
  • Loading branch information
balvig authored Sep 5, 2022
1 parent 48d17db commit 17771bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ $message = new Swift_Message('Hello from Postmark!');
$message->getHeaders()->addTextHeader('X-PM-Message-Stream', 'another-stream');
```

##### 6. Getting the Postmark Message ID after sending
After sending the mail to Postmark, it is possible to get the Postmark ID.

```php
$transport = new \Postmark\Transport('<SERVER_TOKEN>', $defaultHeaders);
$mailer = new Swift_Mailer($transport);

$message = new Swift_Message('Hello from Postmark!');

$mailer->send($message);

$postmarkID = $message->getHeaders()->get('X-PM-Message-Id')->getValue();
```

##### Notes:

- The Transport uses the [Postmark API](https://postmarkapp.com/developer) internally to send mail, via the [/email endpoint](https://postmarkapp.com/developer/api/email-api#send-a-single-email). Other sending features such as Batch sending or sending via Templates are currently not supported by this library.
6 changes: 6 additions & 0 deletions src/Postmark/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
$this->_eventDispatcher->dispatchEvent($responseEvent, 'responseReceived');
}

// Get the Postmark message ID
$jsonResponse = json_decode($response->getBody()->__toString(), true);
if (isset($jsonResponse['MessageID'])) {
$message->getHeaders()->addTextHeader('X-PM-Message-Id', $jsonResponse['MessageID']);
}

if ($sendEvent) {
$sendEvent->setResult($success ? \Swift_Events_SendEvent::RESULT_SUCCESS : \Swift_Events_SendEvent::RESULT_FAILED);
$this->_eventDispatcher->dispatchEvent($sendEvent, 'sendPerformed');
Expand Down
12 changes: 12 additions & 0 deletions tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ public function testPmTagDeaultHeader()

$this->assertEquals('my-tag', $body['Tag']);
}

public function testMessageIdDefaultHeader()
{
$message = new Swift_Message();

$transport = new PostmarkTransportStub([new Response(200, [], '{"MessageID": "MESSAGE_ID"}')]);
$transport->send($message);

$postmarkID = $message->getHeaders()->get('X-PM-Message-Id')->getValue();

$this->assertEquals($postmarkID, "MESSAGE_ID");
}
}


Expand Down

0 comments on commit 17771bd

Please sign in to comment.