Skip to content

Templates

Igor Balos edited this page Oct 1, 2018 · 15 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.

server_token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
client = Postmark::ApiClient.new(server_token)

Get list of available templates

total_count, templates = client.get_templates(count: 2, offset: 0)

templates.first
# => {:active=>true, :template_id=>1, :name=>"Welcome email", :alias=>nil}

Get template by ID

You can get the full template information by using it's ID or alias. You can identify the template by either it's :template_id or :alias.

# get template by ID
client.get_template(1)

# => {:name=>"Welcome", :template_id=>1, :subject=>"Hello from {{product}}!", :html_body=>nil, :text_body=>"This is {{product}} email.", :associated_server_id=>1, :active=>true, :alias=>nil}

Get template by Alias

# get template by Alias
client.get_template('test_alias')

# => {:name=>"Welcome", :template_id=>1, :subject=>"Hello from {{product}}!", :html_body=>nil, :text_body=>"This is {{product}} email.", :associated_server_id=>1, :active=>true, :alias=>"test_alias"}

Create a new template

You can easily create template with create_template method:

client.create_template(name:'Welcome', 
                       alias: 'welcome-v2',
                       subject: 'Welcome email', 
                       html_body: '<html><body>Welcome, {{name}}!</body></html>', 
                       text_body: 'Welcome, {{name}}!')
# => {:template_id=>2, :alias=>"welcome-v2", :name=>"Welcome", :active=>true}

Update template by ID

You can update template by identifying it by ID or Alias and providing template data you wish to update.

client.update_template(1, name: 'Welcome to our product')

# => {:template_id=>1, :alias=>null, :name=>"Welcome to our product", :active=>true}

Update template by Alias

client.update_template('welcome-v2', name: 'Welcome to our product')

# => {:template_id=>2, :alias=>"welcome-v2", :name=>"Welcome to our product", :active=>true}

Delete template by ID

client.delete_template(2)

# => {:error_code=>0, :message=>"Template 2 removed."}

Delete template by Alias

client.delete_template('template_alias')

# => {:error_code=>0, :message=>"Template 2 removed."}

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

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.

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