An official Swiftmailer Transport for Postmark.
Send mail through Postmark from your favorite PHP frameworks!
You're just steps away from super simple sending via Postmark:
composer require wildbit/swiftmailer-postmark
<?php
//import the transport from the standard composer directory:
require_once('./vendor/autoload.php');
$transport = new \Postmark\Transport('<SERVER_TOKEN>');
$mailer = new Swift_Mailer($transport);
//Instantiate the message you want to send.
$message = (new Swift_Message('Hello from Postmark!'))
->setFrom(['[email protected]' => 'John Doe'])
->setTo(['[email protected]'])
->setBody('<b>A really important message from our sponsors.</b>', 'text/html')
->addPart('Another important message from our sponsors.','text/plain');
//Add some attachment data:
$attachmentData = 'Some attachment data.';
$attachment = new Swift_Attachment($attachmentData, 'my-file.txt', 'application/octet-stream');
$message->attach($attachment);
//Send the message!
$mailer->send($message);
?>
$transport = new \Postmark\Transport('<SERVER_TOKEN>');
$transport->registerPlugin(new \Postmark\ThrowExceptionOnFailurePlugin());
$message = new Swift_Message('Hello from Postmark!');
$mailer->send($message); // Exception is throw when response !== 200
You can set default headers at Transport-level, to be set on every message, unless overwritten.
$defaultHeaders = ['X-PM-Tag' => 'my-tag'];
$transport = new \Postmark\Transport('<SERVER_TOKEN>', $defaultHeaders);
$message = new Swift_Message('Hello from Postmark!');
// Overwriting default headers
$message->getHeaders()->addTextHeader('X-PM-Tag', 'custom-tag');
By default, the "outbound" transactional stream will be used when sending messages.
// Change the default stream for every message via Default Headers
$transport = new \Postmark\Transport('<SERVER_TOKEN>', ['X-PM-Message-Stream' => 'your-custom-stream']);
$message = new Swift_Message('Hello from Postmark!');
// Overwrite the default stream for a specific message by setting the header
$message->getHeaders()->addTextHeader('X-PM-Message-Stream', 'another-stream');
After sending the mail to Postmark, it is possible to get the Postmark ID.
$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();
- The Transport uses the Postmark API internally to send mail, via the /email endpoint. Other sending features such as Batch sending or sending via Templates are currently not supported by this library.