首页
学习
活动
专区
圈层
工具
发布

Mandrill
EN

Stack Overflow用户
提问于 2013-07-17 20:39:13
回答 7查看 14.9K关注 0票数 12

很高兴能在StackOverflow上问我的第一个问题。多年来,我一直依靠它来教自己很多东西!

我的问题是。我在尝试通过Mandrill的API发送邮件时遇到以下错误:

代码语言:javascript
复制
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}

下面的代码是我用来尝试发送邮件的代码:

代码语言:javascript
复制
<?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密钥,它是有效的!

希望有人能帮上忙,并感谢你在这里表现得很棒!

谢谢!

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2013-07-17 21:55:13

您可能还希望只使用数组,让PHP为您处理JSON编码。如果JSON由于某种原因无效,这种特殊的错误很常见。因此,例如,您可以像这样设置参数:

代码语言:javascript
复制
$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来解析响应。

票数 15
EN

Stack Overflow用户

发布于 2013-12-01 08:14:36

Bansi的回答对Dan B有效,但如果其他人也有同样的问题,最好检查内容是否有特殊字符(重音、变音、塞迪拉、撇号等)。如果是这种情况,解决方案可能是对文本进行utf8编码:

代码语言:javascript
复制
$content = utf8_encode('<p>Greetings from Bogotá, Colombia. Att:François</p>');
票数 12
EN

Stack Overflow用户

发布于 2013-07-17 20:46:31

我不知道",但是您的$content字符串中包含双引号",并且$postString中的分隔符也是双引号。这在任何语言中都会中断。您需要按照mandril的要求转义$content中的双引号。

"html": "' . $content . '",将转换为

代码语言:javascript
复制
"html": "<p>this is the emails html <a href="www.google.co.uk">content</a></p>",
                                            ^                ^

试一试

代码语言:javascript
复制
 "html": "' . str_replace('"','\\"',$content) . '",
 "text": "' . str_replace('"','\\"',$content_text) . '",

而不是

代码语言:javascript
复制
 "html": "' . $content . '",
 "text": "' . $content_text . '",
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17700002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档