-1

I'm trying to create a webhook for Mandrill that will send an e-mail to the sender when a previously sent e-mail bounces. I'm able to receive the JSON data from Mandrill, but am unable to parse that data to send to the original sender:

<?php
require_once 'mandrill-api-php/src/Mandrill.php'; //Not required with Composer
$mandrill = new Mandrill('*myapikey*');

$json = stripslashes($_POST['mandrill_events']);
$jsondata = json_decode($json,true);

$subject = $jsondata['event'];
$message = "STRIPSLASHES: ".$json."----JSONDATA----".$jsondata;

$emailAddress = "*me@mydomain.com*";
mail($emailAddress, $subject, $message);
?>

Here is what the $json data looks like in the $message variable. It is a literal copy and paste from the test e-mail I receive:

STRIPSLASHES: [{"event":"spam","msg":{"ts":1365109999,"subject":"This an example webhook message","email":"example.webhook@mandrillapp.com","sender":"example.sender@mandrillapp.com","tags":["webhook-example"],"opens":[{"ts":1365111111}],"clicks":[{"ts":1365111111,"url":"http://mandrill.com"}],"state":"sent","metadata":{"user_id":111},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa","_version":"exampleaaaaaaaaaaaaaaa"},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa","ts":1422475458},{"event":"spam","msg":{"ts":1365109999,"subject":"This an example webhook message","email":"example.webhook@mandrillapp.com","sender":"example.sender@mandrillapp.com","tags":["webhook-example"],"opens":[{"ts":1365111111}],"clicks":[{"ts":1365111111,"url":"http://mandrill.com"}],"state":"sent","metadata":{"user_id":111},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1","_version":"exampleaaaaaaaaaaaaaaa"},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1","ts":1422475458}]----JSONDATA----Array

I notice that the $json is outputting the json data, but has a leading and ending bracket, as opposed to starting with a squiggly bracket. So I decided to call the data as if it were an array, but to no avail.

In a test, instead of doing $json = stripslashes(... I copy and pasted the json data above as a literal string. Once I removed the leading/ending brackets, I was able to parse some data.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    maybe convert it to a string (using stringify) then remove / replace those leading / ending brackets, then parse the json? I had a similar issue on a project in the past and had to either escape special characters within my values or change the type and try to parse the json. good luck with it. – tamak Jan 28 '15 at 21:42
  • 1
    From square bracket to square bracket the JSON is valid. http://jsonlint.com `STRIPSLASHES:` is not valid JSON, it lacks quotes. – Jay Blanchard Jan 28 '15 at 21:43
  • `echo $jsondata[0]['msg']['sender'];` – PhearOfRayne Jan 28 '15 at 21:45
  • Wheres the problem with an Array? It could be multiple events, so it has to be an array? – dognose Jan 28 '15 at 21:45
  • 3
    _“Once I removed the leading/ending brackets, I was able to parse some data”_ – getting the data _parsed_ is not your problem here; it’s using the right syntax to access elements in the resulting data structure. So use `var_dump` to make a debug output of the parsed result to get a better understanding of what you are actually dealing with. – CBroe Jan 28 '15 at 21:46

2 Answers2

0

Why don't you try removing the brackets using PHP?

$json = ltrim($json, "[");
$json = rtrim($json, ']");

And then pass it to the decoder?

Gratus D.
  • 787
  • 7
  • 22
0

Actually, I was able to fix it by "grabbing" the json data a different way, formatting it correctly as I receive it:

$rawdata = file_get_contents('php://input');
$decodeurl = urldecode($data);
$jsonready = substr($decodeurl, 16);
$data = json_decode($jsonready, true);

$recipient = $data['0']['msg']['email'];
//etc, etc, etc

I followed this example:

https://sendy.co/forum/discussion/1137/using-mandrill-webhook-for-bounces-complaints/p1

I hope this helps people who are trying to utilize Mandrill's API!