Mandrill ValidationError

I am very glad that I asked my first question about StackOverflow. I relied on this to teach me a lot over the years!

My question is that. I get the following error when trying to send mail through the Mandrill API:

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

The following code is what I use to try to send 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; ?> 

Which may cause a verification error in the message. I provide my API key and it is valid!

Hope someone can help, and thanks for being generally AWESOME here!

Thanks!

+7
json php mandrill
source share
5 answers

You can also just use arrays and let PHP handle JSON encoding for you. This particular error is common if JSON is invalid for some reason. So, for example, you can set the following parameters:

 $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.

+11
source share

Bansi's answer worked for Dan B, but if anyone has the same problem, it's good to check if the content has special characters (accents, umlauts, cedillas, apostrophes, etc.). If in this case the solution may be utf8, then encode the text:

 $content = utf8_encode('<p>Greetings from Bogotá, Colombia. Att:François</p>'); 
+9
source share

I don’t know about mandrill, but your $content string contains double quotes " and your separator in $postString also double quotes. This will break in any language. You need to avoid double quotes in $content as mandril requires.

"html": "' . $content . '", will be translated into

 "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

  "html": "' . $content . '", "text": "' . $content_text . '", 
+2
source share

Also you need to remove new lines from the html code:

$content = trim(preg_replace('/\s+/', ' ', $content));

0
source share

We experimented with installing Dan curl to publish Mandrill-enriched html messages, but this time using html in the template_content array: [] messages / send -template.json api.

What I noticed is that this setting (Bansi fix enabled) seemed to work in Mandrill, try the page: https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template p >

But in my php script I kept getting this stubborn error You must specify a key value . Apparently, thanks to this thread , I solved the problem by adding utf8 to 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); 
0
source share

All Articles