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
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.
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:
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.
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);
In Laravel 7.x, Mandrill is no longer part of core. If, like me, you are using therobfonz/laravel-mandrill-driver
, you might also see a similar error if you fail to set Mandril key in your .env file.
I had this
MANDRILL_SECRET=h3xxxx
but I needed this MANDRILL_KEY=h3xxxx
.
To verify which key you are calling, look for this chunk in config\services.php
// Mandrill is no longer core laravel...
'mandrill' => [
'secret' => env('MANDRILL_KEY'),
],
Another symptom of not calling your key is that your failed calls will not show up in your Mandrill api-log listings (since the missing key means Mandrill.com doesn't know where to post the failed attempt).
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 . '",
Also you need to be remove new lines from html code:
$content = trim(preg_replace('/\s+/', ' ', $content));