-
Notifications
You must be signed in to change notification settings - Fork 21
Templates
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 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();
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);
String templateAlias = "mytemplate"
// get template by Alias
Template template = client.getTemplate(templateAlias);
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);
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);
TemplateContent template = new TemplateContent();
templateContent.setName("new name");
// update template
BaseTemplate response = client.setTemplate(templateId, templateContent);
String templateAlias = "mytemplate"
TemplateContent template = new TemplateContent();
templateContent.setName("new name");
// update template
BaseTemplate response = client.setTemplate(templateAlias, templateContent);
String response = client.deleteTemplate(templateId);
String templateAlias = "mytemplate"
String response = client.deleteTemplate(templateAlias);
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());
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());
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);
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);
To send email with template and with attachments, you would do something like this:
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);
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.
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("[email protected]", "[email protected]", "welcome-email");
message.addAttachment("/Users/yourusername/Documents/Temp/yourImageFile.jpg", "cid:image.jpg");
client.deliverMessageWithTemplate(message);
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(messages);
For additional information about the capabilities of the Postmark API, see Postmark Developers Documentation.