Skip to content

Templates Calls

Introduction

In the Mailjet API, Templates are handled with the [/template](/docs/api-reference/email-api/categories/templates/) resource and it's sub-resource [/template/$id/detailcontent](/docs/api-reference/email-api/categories/templates/).

/template is the definition of the basic template information.

/template/$id/detailcontent stores the content of the template.

For more information, visit Transactional templating guide.

Add

Info

Maps to /templates/add.json

Adding a template is done in 2 steps:

  • creating the template
  • updating the content

Creating the template

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$body = [
    'Name' => "First Template"
];
$response = $mj->post(Resources::$Template, ['body' => $body]);
$response->success() && var_dump($response->getData());
?>
{
  "Count": 1,
  "Data": [
    {
      "Author": "",
      "Categories": "",
      "Copyright": "",
      "Description": "",
      "EditMode": "",
      "ID": "",
      "IsStarred": "false",
      "Name": "First Template",
      "OwnerId": "5",
      "OwnerType": "",
      "Presets": "",
      "Previews": "",
      "Purposes": ""
    }
  ],
  "Total": 1
}

Issue a POST request to template

Updating the content

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$body = [
    'Html-part' => "Hello <strong>world</strong>!",
    'Text-part' => "Hello world!"
];
$response = $mj->post(Resources::$TemplateDetailcontent, ['id' => $id, 'body' => $body]);
$response->success() && var_dump($response->getData());
?>
{
  "Count": 1,
  "Data": [
    {
      "Html-part": "Hello <strong>world</strong>!",
      "Text-part": "Hello world!"
    }
  ],
  "Total": 1
}

Issue a POST request to template/$ID/detailcontent with the ID to set your content

Info

Info

/templates/info.json

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->get(Resources::$Template, ['id' => $id]);
$response->success() && var_dump($response->getData());
?>
{
  "Count": 1,
  "Data": [
    {
      "Author": "",
      "Categories": "",
      "Copyright": "",
      "Description": "",
      "EditMode": "",
      "ID": "",
      "IsStarred": "false",
      "Name": "First Template",
      "OwnerId": "5",
      "OwnerType": "",
      "Presets": "",
      "Previews": "",
      "Purposes": ""
    }
  ],
  "Total": 1
}

Issue a GET request to template with the ID to get specific template

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->get(Resources::$TemplateDetailcontent, ['id' => $id]);
$response->success() && var_dump($response->getData());
?>
{
  "Count": 1,
  "Data": [
    {
      "Html-part": "Hello <strong>world</strong>!",
      "Text-part": "Hello world!"
    }
  ],
  "Total": 1
}

Issue a GET request to template/$ID/detailcontent with the ID to get specific template content

Update

Info

/templates/update.json

# Modify :
curl -s \
	-X PUT \
	--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
	https://api.mailjet.com/v3/REST/template/$ID \
	-H 'Content-Type: application/json' \
	-d '{
		"Author":"",
		"Categories":"",
		"Copyright":"",
		"Description":"",
		"EditMode":"",
		"IsStarred":"false",
		"Presets":"",
		"Purposes":""
	}'

Issue a PUT request to template with the ID to get specific template

Delete

Info

/templates/delete.json

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->delete(Resources::$Template, ['id' => $id]);
$response->success() && var_dump($response->getData());
?>

Issue a DELETE request to template with the ID to get specific template

List

Info

/templates/list.json

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->get(Resources::$Template);
$response->success() && var_dump($response->getData());
?>
{
  "Count": 1,
  "Data": [
    {
      "Author": "",
      "Categories": "",
      "Copyright": "",
      "Description": "",
      "EditMode": "",
      "ID": "",
      "IsStarred": "false",
      "Name": "First Template",
      "OwnerId": "5",
      "OwnerType": "",
      "Presets": "",
      "Previews": "",
      "Purposes": ""
    }
  ],
  "Total": 1
}

Issue a GET request to template

Migrate your templates

Mailjet Templating Language uses a syntax closer to templating language like Jinja2 (Python) or Twig (PHP).

FeatureMandrill syntaxMailjet syntaxComments
Conditions{{#if condition}}...{/if}}{% if condition %}...{% endif %}
Loops{{#each list}}...{{this}}...{{/each}}{% for foo in bar %}{{foo}}{% endfor %}
Nested Variables{{foo.bar}}{{foo.bar}}
Variables{{foobar}}{{data:foobar}} or {{var:foobar}}Transactional messages can contain both contact properties or message variables