<?php
echo "OptiMonk Webhook Test";
$emailAddress = "";

$webhookContent = "";
// get webhook data
$webhook = fopen('php://input', 'rb');
while (!feof($webhook)) {
    $webhookContent .= fread($webhook, 4096);
}
fclose($webhook);

// convert webhook data to Subscriber array
$rawData = explode("&", urldecode($webhookContent));
foreach ($rawData as $field) {
    $temp = explode("=", $field);
    $subscriber[$temp[0]] = $temp[1];
}

// set subject and recipient
if ( $emailAddress != "") {
    $to = $emailAddress;
    $subject = "New Subscriber!";
} else {
    $to = $subscriber["email"];
    $subject = "[OptiMonk Webhook test] Thank you for subscribing!";
}

$body = "Subscriber data: \n\r";

// write subscriber data in email body
foreach ($subscriber as $key => $value) {
    $body .= $key . ": " . $value . "\n";
}

// send mail
mail($to, $subject, $body);
