非常高兴能在StackOverflow上提出我的第一个问题。这些年来,我一直依靠它来教自己很多!
我的问题是这个。尝试通过Mandrill的API发送邮件时出现以下错误:
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}
下面的代码是我用来尝试发送邮件的代码:
<?php
$to = '[email protected]';
$content = '<p>this is the emails html <a href="www.google.co.uk">content</a></p>';
$subject = 'this is the subject';
$from = '[email protected]';
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$content_text = strip_tags($content);
$postString = '{
"key": "RR_3yTMxxxxxxxx_Pa7gQ",
"message": {
"html": "' . $content . '",
"text": "' . $content_text . '",
"subject": "' . $subject . '",
"from_email": "' . $from . '",
"from_name": "' . $from . '",
"to": [
{
"email": "' . $to . '",
"name": "' . $to . '"
}
],
"track_opens": true,
"track_clicks": true,
"auto_text": true,
"url_strip_qs": true,
"preserve_recipients": true
},
"async": false
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
echo $result;
?>
什么可能导致消息中的验证错误。我提供我的API密钥,它是有效的!
希望有人能够提供帮助,并且感谢您在这里一般都很棒!
谢谢!
您可能还想使用数组,让PHP为您处理JSON编码。如果JSON由于某种原因无效,则此特定错误很常见。因此,例如,您可以像这样设置参数:
$params = array(
"key" => "keyhere",
"message" => array(
"html" => $content,
"text" => $content_text,
"to" => array(
array("name" => $to, "email" => $to)
),
"from_email" => $from,
"from_name" => $from,
"subject" => $subject,
"track_opens" => true,
"track_clicks" => true
),
"async" => false
);
$postString = json_encode($params);
如果需要,您还可以使用json_decode
来解析响应。
Bansi的答案适用于Dan B,但如果其他人遇到同样的问题,那么检查内容是否有特殊字符(重音符号,变音符号,cedillas,撇号等)是很好的。如果是这种情况,解决方案可能是utf8编码文本:
$content = utf8_encode('<p>Greetings from Bogotá, Colombia. Att:François</p>');
我不知道mandrill,但你的$content
字符串中有双引号"
,你在$postString
的分隔符也是双引号。这将以任何语言打破。你需要按照mandril的要求逃避$content
中的双引号。
"html": "' . $content . '",
将翻译成
"html": "<p>this is the emails html <a href="www.google.co.uk">content</a></p>",
^ ^
尝试
"html": "' . str_replace('"','\\"',$content) . '",
"text": "' . str_replace('"','\\"',$content_text) . '",
代替
"html": "' . $content . '",
"text": "' . $content_text . '",
PHP Mandrill API错误:“Mandrill_ValidationError - 您必须指定键值”
此错误还可能表示json_encode()无法编码并返回空字符串。当空字符串通过curl提交给Mandrill时,它无法让您知道它有完全空的POST内容,而是发出有用的消息“您必须指定一个键值”。
显然,通过在几个级别进行更好的检测可以减轻这个问题:
这一点都没有完成,因此调试起来不必要。
我的案例中的简单修复是在包含图像内容之前有效地更改代码以运行base64_encode(),即:
'content' => base64_encode(file_get_content("image.jpg"),
如上所述,更好的解决方法是升级Mandrill.php API文件以检测编码失败并抛出错误。
您还需要从html代码中删除新行:
$content = trim(preg_replace('/\s+/', ' ', $content));
一直在尝试使用Dan的curl设置来向Mandrill发布html丰富的消息,但这次在message / send-template.json api的template_content:[]数组中使用了html。
我注意到这个设置(包括Bansi修复)似乎在Mandrill的试用页面中有效:https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template
但在我的PHP脚本中,我一直收到这个顽固的You must specify a key value
错误。非常感谢this thread,我通过在请求标头中添加utf8作为charset解决了这个问题:
$ch = curl_init();
$headers = array("Content-type: application/json;charset=\"utf-8\"");
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);