Mandrill ValidationError

问题描述 投票:8回答:6

非常高兴能在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 mandrill
6个回答
11
投票

您可能还想使用数组,让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来解析响应。


11
投票

Bansi的答案适用于Dan B,但如果其他人遇到同样的问题,那么检查内容是否有特殊字符(重音符号,变音符号,cedillas,撇号等)是很好的。如果是这种情况,解决方案可能是utf8编码文本:

$content = utf8_encode('<p>Greetings from Bogotá, Colombia. Att:François</p>');

3
投票

我不知道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 . '",

1
投票

PHP Mandrill API错误:“Mandrill_ValidationError - 您必须指定键值”

此错误还可能表示json_encode()无法编码并返回空字符串。当空字符串通过curl提交给Mandrill时,它无法让您知道它有完全空的POST内容,而是发出有用的消息“您必须指定一个键值”。

显然,通过在几个级别进行更好的检测可以减轻这个问题:

  • API级别的Mandrill可能会返回“空POST”之类的错误
  • Mandrill.php API类可能会返回错误,如“json_encode失败,可能的非base64图像或内容问题”
  • Mandrill.php API类可以检查图像中的非base64内容,并给出类似“未编码的图像”的错误
  • 如果json_encode()抛出某种错误(不确定这个)会有点好

这一点都没有完成,因此调试起来不必要。

我的案例中的简单修复是在包含图像内容之前有效地更改代码以运行base64_encode(),即:

          'content' => base64_encode(file_get_content("image.jpg"),

如上所述,更好的解决方法是升级Mandrill.php API文件以检测编码失败并抛出错误。


0
投票

您还需要从html代码中删除新行:

$content = trim(preg_replace('/\s+/', ' ', $content));


0
投票

一直在尝试使用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);
© www.soinside.com 2019 - 2024. All rights reserved.