Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 31
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.
ApiClientclient = Postmark.getApiClient(<servertoken>);Get list of templates and layouts
Templatestemplates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4));
// get name of the first template in the listtemplates.getTemplates().get(0).getName();Get list of templates connected to certain Layout template.
Templatestemplates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4).build("layoutTemplate", "layout-basic"));
// get name of the first template in the listtemplates.getTemplates().get(0).getName();To get a list of template layouts, you can filter by Layout type.
Templatestemplates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4).build("templateType",TemplateTypes.Layout.value));
// get name of the first template in the listtemplates.getTemplates().get(0).getName();Templatestemplates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4));
IntegertemplateId = templates.getTemplates().get(0).getTemplateId();
// get template by IDTemplatetemplate = client.getTemplate(templateId);StringtemplateAlias = "mytemplate"// get template by AliasTemplatetemplate = client.getTemplate(templateAlias);TemplateContenttemplate = newTemplateContent();
template.setHtmlBody("test html");
template.setTextBody("test text");
template.setName("name");
template.setSubject("subject");
// create a new templateBaseTemplateresponse = client.createTemplate(template);TemplateContenttemplate = newTemplateContent();
template.setTemplateType(TemplateTypes.Layout);
template.setHtmlBody("test {{{@content}}} html");
template.setTextBody("test {{{@content}}} text");
template.setName("name");
template.setSubject("subject");
// create a new layoutBaseTemplateresponse = client.createTemplate(template);TemplateContenttemplate = newTemplateContent();
templateContent.setName("new name");
// update template BaseTemplateresponse = client.setTemplate(templateId, templateContent);StringtemplateAlias = "mytemplate"TemplateContenttemplate = newTemplateContent();
templateContent.setName("new name");
// update template BaseTemplateresponse = client.setTemplate(templateAlias, templateContent);Stringresponse = client.deleteTemplate(templateId);StringtemplateAlias = "mytemplate"Stringresponse = client.deleteTemplate(templateAlias);TemplateToValidatetemplateToValidate = newTemplateToValidate();
templateToValidate.setSubject("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setHtmlBody("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setTextBody("{{#company}}{{phone}}{{/company}}{{#each person}} {{name}} {{/each}}");
// set model as HashMapHashMaprenderModel = newHashMap<String, Object>();
renderModel.put("userName", "bobby joe");
templateToValidate.setTestRenderModel(renderModel);
// validate templateTemplateValidationvalidation = client.validateTemplate(templateToValidate);
System.out.println(validation.getHtmlBody().getContentIsValid());TemplateToValidatetemplateToValidate = newTemplateToValidate();
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 HashMapHashMaprenderModel = newHashMap<String, Object>();
renderModel.put("userName", "bobby joe");
templateToValidate.setTestRenderModel(renderModel);
// validate templateTemplateValidationvalidation = client.validateTemplate(templateToValidate);
System.out.println(validation.getHtmlBody().getContentIsValid());Model will be in this case provided as a string.
TemplatedMessagemessage = newTemplatedMessage("igor@example.com", "tema@example.com");
message.setTemplateId(templateId);
//set model as StringStringmodelString = "{\"total\" : \"totalValue\"}";
message.setTemplateModel(modelString);
// send email with templateMessageResponseresponse = client.deliverMessageWithTemplate(message);Model will be in this case provided as a Hash Map.
TemplatedMessagemessage = newTemplatedMessage("igor@example.com", "tema@example.com");
message.setTemplateId(templateId);
// set model as HashMapHashMapmodel = newHashMap<String, Object>();
model.put("product_name", "hey product");
model.put("total", "totalValue");
model.put("name", "nameValue");
message.setTemplateModel(model);
MessageResponseresponse = client.deliverMessage(message);To send email with template and with attachments, you would do something like this:
TemplatedMessagemessage = newTemplatedMessage("igor@example.com", "tema@example.com");
message.setTemplateId(templateId);
// set model as HashMapHashMapmodel = newHashMap<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");
MessageResponseresponse = 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:
TemplatedMessagemessage = newTemplatedMessage("igor@example.com", "john@example.com", "welcome-email");
message.addAttachment("/Users/yourusername/Documents/Temp/yourImageFile.jpg", "cid:image.jpg");
client.deliverMessageWithTemplate(message);ArrayList<TemplatedMessage> messages = newArrayList<>();
messages.add(newTemplatedMessage("igor@example.com", "tema@example.com", templateId));
messages.add(newTemplatedMessage("igor@example.com", "milan@example.com", templateId));
// send batch email with templateArrayList<MessageResponse> responses = client.deliverMessageWithTemplate(messages);For additional information about the capabilities of the Postmark API, see Postmark Developers Documentation.