-
Notifications
You must be signed in to change notification settings - Fork 64
Using Templates
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(
"sender@example.com",
"recipient@example.com",
"template-alias",
["name" => "John Smith"],
);
echo $sendResult->message ."\r\n";
?>A Template ID can also be used to identify the template, instead of using the alias:
$sendResult = $client->sendEmailWithTemplate(
"sender@example.com",
"recipient@example.com",
<template-id>,
["name" => "John Smith"],
);The Postmark-PHP client can be installed from Packagist.
For additional information about the capabilities of the Postmark API, see Postmark Developers Documentation.