Using templates
This page will help you get started with the Template resource
In this guide, you will learn the basics of working with templates in Cakemail.
Create a template
Let's create your first template to get started. To do this, you can call the Create a template endpoint. Review the full API reference for a list of all the parameters you can pass.
curl --request POST \
--url https://api.cakemail.dev/templates \
--header 'Accept: application/json' \
--header 'authorization: Bearer [access token]' \
--header 'Content-Type: application/json' \
--data '{"name":"[template name]","content":{"type":"html","html":"[HTML content]"}}'
from cakemail.models import Template, TemplateContent
my_template = api.template.create(
Template(
name='<template name>',
content=TemplateContent(
type='html',
html='<HTML content>'
)
)
)
<?php
$myTemplate = $api->template->create([
'template' => new \Cakemail\Lib\Model\Template([
'name' => '<template name>',
'content' => new \Cakemail\Lib\Model\TemplateContent([
'type' => 'html',
'html' => '<HTML content>'
])
])
]);
You now have an html template you can use as a starting point for your next campaigns or for sending your transactional emails.
Update a template
Once you have a template, you can update it from time to time by using the Update a template endpoint.
curl --request PATCH \
--url https://api.cakemail.dev/templates/[template ID] \
--header 'Accept: application/json' \
--header 'authorization: Bearer [access token]' \
--header 'Content-Type: application/json' \
--data '{"name":"[new template name]"}'
from cakemail.models import PatchTemplate
my_template = api.template.update(
template_id=my_template.id,
patch_template=PatchTemplate(
name='<new template name>',
)
)
<?php
$myTemplate = $api->template->update([
'template_id' => $myTemplate['id'],
'patch_template' => new \Cakemail\Lib\Model\PatchTemplate([
'name' => '<new template name>'
])
])
Next, let's try to use our template in a campaign.
Using a template in a campaign
Now that you have a template, you can use it when creating a new campaign.
curl --request POST \
--url https://api.cakemail.dev/campaigns \
--header 'Accept: application/json' \
--header 'authorization: Bearer [access token]' \
--header 'Content-Type: application/json' \
--data '{"name":"<new tempalte name>","content":{"type":"html","template":{"id":[template ID]}}}'
from cakemail.models import CreateCampaign, CampaignContent, TemplateId
my_campaign = api.campaign.create(
CreateCampaign(
name='<template name>',
content=CampaignContent(
type='html',
template=TemplateId(id=my_template.id)
)
)
)
<?php
$myCampaign = $api->campaign->create([
'create_campaign' => new \Cakemail\Lib\Model\CreateCampaign([
'name' => '<template name>',
'content' => new \Cakemail\Lib\Model\CampaignContent([
'type' => 'html',
'template' => new \Cakemail\Lib\Model\TemplateId([
'id' => $myTemplate['id']
])
])
])
]);
You just created a new campaign using your template.
Using a template with transactional emails
You can also use templates to send transactional emails. Since a template can contain personalization tags, it is possible to also specify custom attributes to be used with the template.
curl --request POST \
--url https://api.cakemail.dev/emails \
--header 'Accept: application/json' \
--header 'authorization: Bearer [access token]' \
--header 'Content-Type: application/json' \
--data '{...,"content":{"template":{"id":[template ID],"custom_attributes":[{"name":"name","value":"[contact name]"}]}}}'
from cakemail.models import Email, EmailContent, TemplateInfo, TemplateCustomAttributes
api.transactional_email.send(
email=Email(
...,
content=EmailContent(
template=TemplateInfo(
id=my_template.id,
custom_attributes=[TemplateCustomAttributes(
name='name',
value='<contact name>'
)]
)
)
)
)...,
<?php
$api->transactional_email->send([
'email' => new \Cakemail\Lib\Model\Email([
...,
'content' => new \Cakemail\Lib\Model\EmailContent([
'template' => new \Cakemail\Lib\Model\TemplateInfo([
'id' => $myTemplate['id'],
'custom_attributes' => [
new \Cakemail\Lib\Model\TemplateCustomAttributes([
'name' => 'name',
'value' => '<contact name>'
])
]
])
])
])
]);
You can explore the full API reference for more advanced features in the Asset API.
Updated over 3 years ago