Skip to content

Instantly share code, notes, and snippets.

@cgsmith
Created May 25, 2016 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cgsmith/db8c8a2e3ad435abc6694a15f42e9bca to your computer and use it in GitHub Desktop.
Save cgsmith/db8c8a2e3ad435abc6694a15f42e9bca to your computer and use it in GitHub Desktop.
SparkPost Hook for PHPList
<?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;
}
}
}
@yargevad
Copy link

yargevad commented Apr 5, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment