Skip to content

Templates

Igor Balos edited this page Apr 3, 2018 · 27 revisions

For these API requests you will need to use a server API token. Once you obtain it, you will need to use server API client.

ApiClient client = Postmark.getApiClient(<server token>);

Get list of available templates

// get list of templates
Templates templates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4));

// get name of the first template in the list
templates.getTemplates().get(0).getName();

Get template by ID

Templates templates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4));
Integer templateId = templates.getTemplates().get(0).getTemplateId();

// get template by ID
Template template = client.getTemplate(templateId);

Create new template

TemplateContent template = new TemplateContent();
template.setHtmlBody("test html");
template.setTextBody("test text");
template.setName("name");
template.setSubject("subject");

// create a new template
BaseTemplate response = client.createTemplate(template);

Update template

TemplateContent template = new TemplateContent();
templateContent.setName("new name");

// update template 
BaseTemplate response = client.setTemplate(templateId, templateContent);

Delete template

String response = client.deleteTemplate(templateId);

Validate template

TemplateToValidate templateToValidate = new TemplateToValidate();
templateToValidate.setSubject("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setHtmlBody("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setTextBody("{{#company}}{{phone}}{{/company}}{{#each person}} {{name}} {{/each}}");

// set model as HashMap
HashMap renderModel = new HashMap<String, Object>();
renderModel.put("userName", "bobby joe");
templateToValidate.setTestRenderModel(renderModel);

// validate template
TemplateValidation validation = client.validateTemplate(templateToValidate);
System.out.println(validation.getHtmlBody().getContentIsValid());

Send email with template. Model will be in this case provided as a string.

TemplatedMessage message = new TemplatedMessage("[email protected]", "[email protected]");
message.setTemplateId(templateId);

//set model as String
String modelString = "{\"total\" : \"totalValue\"}";
message.setTemplateModel(modelString);

// send email with template
MessageResponse response = client.deliverMessageWithTemplate(message);

Send email with template. Model will be in this case provided as a Hash Map.

TemplatedMessage message = new TemplatedMessage("[email protected]", "[email protected]");
message.setTemplateId(templateId);

// set model as HashMap
HashMap model = new HashMap<String, Object>();
model.put("product_name", "hey product");
model.put("total", "totalValue");
model.put("name", "nameValue");

message.setTemplateModel(model);
MessageResponse response = client.deliverMessage(message);

Send batch email with templates.

ArrayList<TemplatedMessage> messages = new ArrayList<>();
messages.add(new TemplatedMessage("[email protected]", "[email protected]", templateId));
messages.add(new TemplatedMessage("[email protected]", "[email protected]", templateId));

// send batch email with template
ArrayList<MessageResponse> responses = client.deliverMessageWithTemplate(message);