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!
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;
?>
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!
Thanks!
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);
You can also use json_decode
to parse the response if needed.
Bansi's answer worked for Dan B, but if someone else is having the same issue is good to check if the content have special characters (accents,umlauts,cedillas,apostrophes,etc). If that's the case the solution could be to utf8 encode the text:
$content = utf8_encode('<p>Greetings from Bogotá, Colombia. Att:François</p>');
I don't know about mandrill, but your $content
string has double quotes"
in it and your delimiter in the $postString
is also double quotes. This is going to break in any language. You need to escape the double quotes in the $content
as required by mandril.
"html": "' . $content . '",
will translate to
"html": "<p>this is the emails html <a href="www.google.co.uk">content</a></p>",
^ ^
Try
"html": "' . str_replace('"','\\"',$content) . '",
"text": "' . str_replace('"','\\"',$content_text) . '",
Instead of
"html": "' . $content . '",
"text": "' . $content_text . '",
The PHP Mandrill API error: "Mandrill_ValidationError - You must specify a key value"
This error can also indicate that json_encode() is failing to encode and returning an empty string. When the empty string is submitted to Mandrill via curl, it fails to let you know that it got totally empty POST content, and instead issues the helpful message "You must specify a key value".
Obviously this problem could be mitigated by better detection at several levels:
- Mandrill at API level could return an error like "Empty POST"
- The Mandrill.php API class could return an error like "json_encode fail, possible non-base64 image or content problem"
- The Mandrill.php API class could check for non-base64 content in images and give an error like "unencoded image"
- It would be kind of nice if json_encode() threw some sort of error (not sure re this one)
None of this is being done at this point, hence this was unnecessarily hard to debug.
The simple fix in my case was to effectively change code to run base64_encode() before including image content, that is:
'content' => base64_encode(file_get_content("image.jpg"),
A better fix is, as above, to upgrade the Mandrill.php API file to detect a failure to encode and throw an error.
Also you need to be remove new lines from html code:
$content = trim(preg_replace('/\s+/', ' ', $content));
Have been experimenting with Dan's curl setup to post html enriched messages to Mandrill, but this time using html in the template_content: [] array of the message/send-template.json api.
What i noticed was that this setup (fix by Bansi included) seemed to work in Mandrill's try out page : https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template
But in my php script, i kept receiving this stubborn You must specify a key value
error. Apparantly thanks to this thread, i solved the issue by adding utf8 as charset to the request headers:
$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);
来源:https://stackoverflow.com/questions/17700002/mandrill-validationerror