Skip to content

Templates

Igor Balos edited this page Mar 17, 2020 · 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 and layouts

Get list of templates and layouts

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 list of templates connected to certain Layout template.

Templates templates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4).build("layoutTemplate", "layout-basic"));

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

To get a list of template layouts, you can filter by Layout type.

Templates templates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4).build("templateType",TemplateTypes.Layout.value));

// 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);

Get template or layout by Alias

String templateAlias = "mytemplate"

// get template by Alias
Template template = client.getTemplate(templateAlias);

Create a 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);

Create a new template layout

TemplateContent template = new TemplateContent();
template.setTemplateType(TemplateTypes.Layout);
template.setHtmlBody("test {{{@content}}} html");
template.setTextBody("test {{{@content}}} text");
template.setName("name");
template.setSubject("subject");

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

Update template by ID

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

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

Update template or layout by Alias

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

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

Delete template by ID

String response = client.deleteTemplate(templateId);

Delete template or layout by Alias

String templateAlias = "mytemplate"
String response = client.deleteTemplate(templateAlias);

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());

Validate layout

TemplateToValidate templateToValidate = new TemplateToValidate();
templateToValidate.setTemplateType(TemplateTypes.Layout);
templateToValidate.setHtmlBody("Text body {{#company}}test{{/company}} {{{@content}}}");
templateToValidate.setTextBody("<html><head></head><body>{{#company}}{{name}}{{/company}} test {{{@content}}}</body></html>");

// 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("igor@example.com", "tema@example.com");
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 and model

Model will be in this case provided as a Hash Map.

TemplatedMessage message = new TemplatedMessage("igor@example.com", "tema@example.com");
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);

To send email with template and with attachments, you would do something like this:

TemplatedMessage message = new TemplatedMessage("igor@example.com", "tema@example.com");
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);
message.addAttachment("test.txt","text content will go here","application/txt");
message.addAttachment("/Path/To/Your/File/file.pdf");

MessageResponse response = client.deliverMessage(message);

For more examples on how to add attachments, check out email sending section.

Send email with template with inline attachment

To send a template with inline images, the process is similar to the one when sending template with attachments.

Only thing you will need to worry about is to add ContentId to the attachment images and to have that same content id within template you are sending. In the template html body you should have piece of code that looks something like <img src=\"cid:image.jpg\"/>. This would be the content id in your html body in the template.

Once you have that you will be able to make a reference between your template html body and the image you added as attachment and show it inline in the emails you send.

Check the code example for details:

TemplatedMessage message = new TemplatedMessage("igor@example.com", "john@example.com", "welcome-email");
message.addAttachment("/Users/yourusername/Documents/Temp/yourImageFile.jpg", "cid:image.jpg");
client.deliverMessageWithTemplate(message);

Send batch email with templates

ArrayList<TemplatedMessage> messages = new ArrayList<>();
messages.add(new TemplatedMessage("igor@example.com", "tema@example.com", templateId));
messages.add(new TemplatedMessage("igor@example.com", "milan@example.com", templateId));

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