Skip to content

Using Templates

Jens Balvig edited this page Jun 30, 2022 · 7 revisions

Postmark provides a very powerful templating system that allows you to define an email's content ahead of time, and then pass just the values that change when you want to send it. This is a particularly useful when you are sending transactional emails (like password resets emails, for example), as the content is largely the same, except for the a user's name, and the reset link.

Templates also automatically inline stylesheets, which makes maintaining them and keeping them looking great in many email clients, a breeze. You can get more detailed information about the templating language (Mustachio), and how the templating system works, here: http://support.postmarkapp.com/article/1077-template-syntax

To use a template, first create it inside of one of your Postmark Servers. After you create the template, take a note of the template's alias, you will use that in the example code below:

<?php
require_once('./vendor/autoload.php');

// Import the Postmark Client Classes
use Postmark\PostmarkClient;
use Postmark\Models\PostmarkAttachment;

// Create Client
$client = new PostmarkClient(<server token>);

// Make a request
$sendResult = $client->sendEmailWithTemplate(
  "[email protected]",
  "[email protected]", 
  "template-alias", 
  ["name" => "John Smith"],
  true, // Inline CSS
  NULL, // Tag
  NULL, // Track opens
  NULL, // Reply To
  NULL, // CC
  NULL, // BCC
  NULL, // Header array
  NULL, // Attachment array
  NULL, // Track links
  NULL, // Metadata array
  NULL // Message stream
);

echo $sendResult->message ."\r\n";
?>

A Template ID can also be used to identify the template, instead of using the alias:

$sendResult = $client->sendEmailWithTemplate(
  "[email protected]",
  "[email protected]", 
  <template-id>, 
  ["name" => "John Smith"], 
);

Sending batches of templated messages:

The Postmark Batch API allows you to send up to 500 messages at a time. To do so, create messages that follow the same names as outlined in the Postmark Developer Documentation.

Please note that the batch endpoints will return a 200-level http status, even when validation for individual messages may fail. Users of these endpoints should check the success and error code for each message in the response from our API (the results are ordered the same as the original messages).

require_once('./vendor/autoload.php');

use Postmark\PostmarkClient;

// Create messages that follow the JSON naming convention referenced above.
$message1 = ['To' => "[email protected]",
             'Cc' => "[email protected]",
             'TemplateID' => 12345, // You must pass either a TemplateID or a TemplateAlias
             'TemplateModel' => ["name" => "John Smith"],
             'From' => "[email protected]"];

$message2 = ['To' => "[email protected]",
             'Cc' => "[email protected]",
             'TemplateAlias' => "my-template-alias",
             'TemplateModel' => ["name" => "John Smith"],
             'From' => "[email protected]"];

$newClient = new PostmarkClient("server_token");

// Pass the messages as an array to the `sendEmailBatchWithTemplate` function.
$responses = $newClient->sendEmailBatchWithTemplate([$message1, $message2]);

// The response from the batch API returns an array of responses for each
// message sent. You can iterate over it to get the individual results of sending.
foreach($responses as $key => $response){
    var_dump($response);
}