Created
May 25, 2016 13:47
-
-
Save cgsmith/db8c8a2e3ad435abc6694a15f42e9bca to your computer and use it in GitHub Desktop.
SparkPost Hook for PHPList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(0); // do not log errors to... anywhere :) | |
$databaseHost = 'localhost'; | |
$databaseName = ''; | |
$databaseUser = ''; | |
$databasePassword = ''; | |
$databaseTable = ''; | |
$phpAuthUser = ''; | |
$phpAuthPass = ''; | |
$domainRedirect = 'https://www.yourdomain.com/sparkposthook.php'; | |
// Check if we are HTTPS | |
if (!isset($_SERVER['HTTPS'])) { | |
header('Location: ' . $domainRedirect); | |
exit; | |
} | |
// Check if we are logged in | |
if (!isset($_SERVER['PHP_AUTH_USER']) || ($_SERVER['PHP_AUTH_USER'] != $phpAuthUser && $_SERVER['PHP_AUTH_PW'] != $phpAuthPass)) { | |
header('WWW-Authenticate: Basic realm="Authentication Required"'); | |
header('HTTP/1.0 401 Unauthorized'); | |
echo 'Unauthorized'; | |
exit; | |
} else { | |
$postdata = file_get_contents("php://input"); | |
$arrayOfObjects = json_decode($postdata); | |
// Loop through array of objects and see what we need to do | |
$dbh = new PDO('mysql:host='.$databaseHost.';dbname='.$databaseName, $databaseUser, $databasePassword); | |
foreach ($arrayOfObjects as $object) { | |
// https://developers.sparkpost.com/api/?_ga=1.135119090.23804209.1461196449#/reference/webhooks/events-samples/samples | |
//delivery, injection, bounce, delay, policy_rejection, out_of_band, open, click, generation_failure, generation_rejection, spam_complaint, list_unsubscribe, link_unsubscribe | |
switch ($object->msys->message_event->type) { | |
case 'bounce': | |
case 'spam_complaint': | |
$sth = $dbh->prepare('UPDATE `'.$databaseTable.'` SET `blacklisted` = 1 WHERE `email` = ?'); | |
$sth->execute(array($object->msys->message_event->rcpt_to)); | |
break; | |
default: | |
break; | |
} | |
switch ($object->msys->unsubscribe_event->type) { | |
case 'list_unsubscribe': | |
case 'link_unsubscribe': | |
$sth = $dbh->prepare('UPDATE `'.$databaseTable.'` SET `blacklisted` = 1 WHERE `email` = ?'); | |
$sth->execute(array($object->msys->unsubscribe_event->rcpt_to)); | |
break; | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! One note: this processor is a bit aggressive for the
bounce
case - only "code 10" bounces need to be added, other types may eventually be deliverable.