Skip to content

Using Templates

Andrew Theken edited this page Apr 17, 2017 · 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 ID, 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(
  "sender@example.com",
  "recipient@example.com", 
  <template-id>, 
  ["name" => "John Smith"], 
);

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

Clone this wiki locally