Templates keep your email designs in one place, so your code only sends an id and a few values.
To create a template in the dashboard
You need the admin or developer role.
- Open Marketing > Templates and choose Add template.
- Fill in:
- Name: for you, for example
Password reset. - From (optional): such as
Acme <hello@acme.com>. A send can set its own. - Subject: such as
Reset your password, {{{NAME|there}}}. - HTML: the body, with variables.
- Plain text (optional).
- Name: for you, for example
- Under Variables, add each key with a type (Text or Number) and an optional Fallback. Leave Fallback empty to make the variable required.
- Save. Copy the template ID from the list.
To create a template with the API
curl https://api.mailcandor.com/v1/templates \
-H "Authorization: Bearer $MAILCANDOR_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Password reset",
"from": "Acme <hello@acme.com>",
"subject": "Reset your password, {{{NAME|there}}}",
"html": "<p>Hi {{{NAME|there}}},</p><p><a href=\"{{{RESET_URL}}}\">Reset your password</a></p>",
"variables": [
{ "key": "NAME", "type": "string", "fallback_value": "there" },
{ "key": "RESET_URL", "type": "string" }
]
}'
The answer is 201 with the template id. Manage templates with GET, PATCH and DELETE /v1/templates/{id} (full-access key).
Variable syntax
{{{NAME}}}puts in the value ofNAME.{{{NAME|there}}}puts in the value, orthereif none is given.
A template needs html, text or both. Values are HTML-escaped when placed in html, so user input cannot break your layout.
To send a template
curl https://api.mailcandor.com/v1/emails \
-H "Authorization: Bearer $MAILCANDOR_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "sam@example.com",
"template": {
"id": "TEMPLATE_ID",
"variables": { "NAME": "Sam", "RESET_URL": "https://acme.com/reset?t=abc" }
}
}'
fromandsubjectcome from the template if you leave them out. If the template has nofrom, add one to the send.from,subjectandreply_toon the send win over the template’s.- You cannot add
htmlortextalongsidetemplate. - Templates work in batch sends too, one
templateper email.
If it does not work
| Error | Fix |
|---|---|
422 “No template with this id on your team.” |
Check the id. Templates belong to one team. |
422 missing_required_field “Template … needs the variable RESET_URL.” |
Give that variable, or add a fallback to the template. |
422 “Template … has no variable …” |
You sent a variable the template does not declare. Add it to the template or remove it from the send. |
422 “Send template or html/text, not both.” |
Remove html and text. |
422 “template.variables is an object of names to values.” |
Send {"NAME": "Sam"}, not a list. |
422 missing_required_field on from |
The template has no from; add one to the send. |
Full reference: docs.mailcandor.com.