山d ValidationError [英] Mandrill ValidationError

查看:118
本文介绍了山d ValidationError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Very excited to be asking my first question on StackOverflow. I've been relying on it to teach myself quite a lot over the years!

我的问题是这个.尝试通过Mandrill的API发送邮件时出现以下错误:

My question is this. I am getting the following error when trying to send a mail through Mandrill's API:

{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}

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

The code that follows is what I am using to try to send the mail:

<?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密钥,并且有效!

What could be causing the validation error in the message. I am providing my API key, AND it's valid!

希望有人能够为您提供帮助,感谢您在这里的表现真棒!

Hope someone will be able to help, and thanks for generally being AWESOME here!

谢谢!

推荐答案

您可能还想只使用数组,然后让PHP为您处理JSON编码.如果JSON由于某种原因无效,则此特定错误很常见.因此,例如,您可以这样设置参数:

You may also want to just use arrays, and let PHP handle the JSON encoding for you. This particular error is common if the JSON is invalid for some reason. So, for example, you could set your parameters like this:

$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来解析响应.

You can also use json_decode to parse the response if needed.

这篇关于山d ValidationError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆