很高兴能在StackOverflow上问我的第一个问题。多年来,我一直依靠它来教自己很多东西!
我的问题是。我在尝试通过Mandrill的API发送邮件时遇到以下错误:
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}下面的代码是我用来尝试发送邮件的代码:
<?php
$to = 'their@email.com';
$content = '<p>this is the emails html <a href="www.google.co.uk">content</a></p>';
$subject = 'this is the subject';
$from = 'my@email.com';
$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密钥,它是有效的!
希望有人能帮上忙,并感谢你在这里表现得很棒!
谢谢!
发布于 2013-07-17 21:55:13
您可能还希望只使用数组,让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来解析响应。
发布于 2013-12-01 08:14:36
Bansi的回答对Dan B有效,但如果其他人也有同样的问题,最好检查内容是否有特殊字符(重音、变音、塞迪拉、撇号等)。如果是这种情况,解决方案可能是对文本进行utf8编码:
$content = utf8_encode('<p>Greetings from Bogotá, Colombia. Att:François</p>');发布于 2013-07-17 20:46:31
我不知道",但是您的$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 . '",https://stackoverflow.com/questions/17700002
复制相似问题