问题描述
我用 mailchimp/transactional 替换了 mandrill/mandrill 包。我还有一个名为 MYTEMPLATE-001-GR
的模板,带有 mergevar name
。
我尝试使用该模板发送电子邮件:
<?PHP
require_once '/path/to/MailchimpTransactional/vendor/autoload.PHP';
$mailchimp = new MailchimpTransactional\apiclient();
$mailchimp->setApiKey('YOUR_API_KEY');
$message = [
'subject' => 'Lorem Ipsum','from_email' => 'user@example.com','from_name' => 'Example User','to' => 'user@example.com'
'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);
但响应如下:
"""
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"}
"""
意思是我应该在 template_content
中提供一些值,但我的模板没有输入字段,只是合并变量,因此我不知道在这种情况下需要提供什么值。
之前的版本只是使用 NULL 作为模板内容值并且工作得很好。你知道我怎么把电话改成更高版本吗?
解决方法
在这种情况下,基于此 tweet 的 template_content
用于 sendTemplate
Mandrill APi 调用应包含以下值:
'template_content'=>[["name"=>'','content'=>'']],
因此您的代码应如下所示:
require_once '/path/to/MailchimpTransactional/vendor/autoload.php';
$mailchimp = new MailchimpTransactional\ApiClient();
$mailchimp->setApiKey('YOUR_API_KEY');
$message = [
'subject' => 'Lorem Ipsum','from_email' => 'user@example.com','from_name' => 'Example User','to' => 'user@example.com'
'global_merge_vars' => ['name'=>'Chuck Norris'],'track_opens' => true,'track_clicks' => true
];
$response = $mailchimp->messages->sendTemplate([
"template_name" => "MYTEMPLATE-001-GR","template_content"=>[["name"=>'',"message" => $message,]);
print_r($response);