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.