Search code examples
phpmailchimpmandrill

What the template_content should be in case that template itself has no need for it (eg template has no mc:edit)?


I replaced the mandrill/mandrill package with mailchimp/transactional. Also I have a tamplate named MYTEMPLATE-001-GR with mergevar name.

And I try to send an email using that template:

<?php
require_once '/path/to/MailchimpTransactional/vendor/autoload.php';

$mailchimp = new MailchimpTransactional\ApiClient();
$mailchimp->setApiKey('YOUR_API_KEY');

$message = [
  'subject' => 'Lorem Ipsum',
  'from_email' => '[email protected]',
  'from_name'  => 'Example User',
  'to'   => '[email protected]'
  'global_merge_vars' => ['name'=>'Chuck Norris'],
  'track_opens' => true,
  'track_clicks' => true
];

$response = $mailchimp->messages->sendTemplate([
    "template_name" => "MYTEMPLATE-001-GR",
    "template_content" => [[]],
    "message" => $message,
]);
print_r($response);

But the response is the following:

"""
HTTP/1.1 500 Internal Server Error\r\n
server: nginx/1.4.6 (Ubuntu)\r\n
date: Tue, 22 Jun 2021 13:51:38 GMT\r\n
content-type: application/json; charset=utf-8\r\n
transfer-encoding: chunked\r\n
access-control-allow-origin: *\r\n
access-control-allow-methods: POST, GET, OPTIONS\r\n
access-control-allow-headers: Content-Type\r\n
access-control-allow-credentials: false\r\n
\r\n
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a template_content value"}
"""

Meaning I should provide some value in template_content but my template has NO input fields just merge vars therefore I do not know what value needs to be provided in this case.

The previous version it just used NULL as template content value and worked just fine. Do you know how I can change the call into the later version?


Solution

  • In this case the template_content based upon this tweet for the sendTemplate Mandrill APi call should contain the following values:

     'template_content'=>[["name"=>'','content'=>'']],
    

    Therefore your code should be the following:

    require_once '/path/to/MailchimpTransactional/vendor/autoload.php';
    
    $mailchimp = new MailchimpTransactional\ApiClient();
    $mailchimp->setApiKey('YOUR_API_KEY');
    
    $message = [
      'subject' => 'Lorem Ipsum',
      'from_email' => '[email protected]',
      'from_name'  => 'Example User',
      'to'   => '[email protected]'
      'global_merge_vars' => ['name'=>'Chuck Norris'],
      'track_opens' => true,
      'track_clicks' => true
    ];
    
    $response = $mailchimp->messages->sendTemplate([
        "template_name" => "MYTEMPLATE-001-GR",
        "template_content"=>[["name"=>'','content'=>'']],
        "message" => $message,
    ]);
    print_r($response);