Skip to content

Commit

Permalink
FBL improvements
Browse files Browse the repository at this point in the history
- Extracting listid/subscriber-id from FBL message
- Reworked FBL-processor to include more status information
  • Loading branch information
Gerd Naschenweng committed Jun 8, 2016
1 parent b3f99cf commit 548fafe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
15 changes: 10 additions & 5 deletions providers/bounce-provider-interspire.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function Interspire_unsubscribeRecipient($recipient) {
}

if ($INTERSPIRE_HANDLER_ENABLED == false) {
return false;
return array(false, "Interspire not enabled! Check logs!");
}

// Get the interspire lists
Expand All @@ -104,23 +104,28 @@ function Interspire_unsubscribeRecipient($recipient) {

if (is_null($apiInterspireListIDs) || empty($apiInterspireListIDs)) {
$log->lwrite(' Interspire: Unable to unsubscribe user ' . $recipient . ', Interspire lists are empy!');
return false;
return array(false, "Interspire lists are empty");
}

// Get all lists for email recipient
$emailLists = Interspire_getAllListsForEmailAddress($recipient);

if (is_null($emailLists) || empty($emailLists)) {
$log->lwrite(' Interspire: Skipping recipient ' . $recipient . ' - no subscribed lists returned');
return false;
return array(true, "Skipped - not subscribed");
}

$unsubMessage = "";

// Iterate through users lists and unsubscribe
foreach ($emailLists as $listid) {
$log->lwrite(' Interspire: Unsubscribe user ' . $recipient . ' from list=' . $listid . ', status=' . Interspire_unsubscribeSubscriber($recipient, $listid));
$unsubStatus = Interspire_unsubscribeSubscriber($recipient, $listid);
$log->lwrite(' Interspire: Unsubscribe user ' . $recipient . ' from list=' . $listid . ', status=' . $unsubStatus);

$unsubMessage .= "List-" . $listid . "=" . $unsubStatus . ",";
}

return true;
return array(true, $unsubMessage);
}

// Get Interspire lists - this is needed to individually unsubscribe users
Expand Down
35 changes: 33 additions & 2 deletions providers/bounce-provider-mailwizz.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ function MailWizz_unsubscribeRecipient($recipient) {
global $log, $MailWizzEndPoint, $MAILWIZZ_HANDLER_ENABLED;

if ($MAILWIZZ_HANDLER_ENABLED == false) {
return false;
return array(false, "MailWizz not enabled! Check logs!");
}

$unsubscribeSuccess = false;
$unsubMessage = "";

// Check if subscriber exists
$response = $MailWizzEndPoint->emailSearchAllLists($recipient, $pageNumber = 1, $perPage = 30);
Expand All @@ -89,19 +90,49 @@ function MailWizz_unsubscribeRecipient($recipient) {
foreach ($response->body['data']['records'] as $subscription) {
if ($subscription['status'] != "unsubscribed") {
$unsubscriberesponse = $MailWizzEndPoint->unsubscribe($subscription['list']['list_uid'], $subscription['subscriber_uid']);
$unsubMessage .= $unsubscriberesponse->body['status'] . "=" . $subscription['list']['name'] . " ";
$log->lwrite(' - ' . $unsubscriberesponse->body['status'] . ': ' . $subscription['list']['name']);
$unsubscribeSuccess = true;
} else {
$log->lwrite(' - skipped: ' . $subscription['list']['name']);
$unsubMessage .= "skipped=" . $subscription['list']['name'] . " ";
$unsubscribeSuccess = true;
}
}
} else {
if ($response->body['status'] != "success") {
$log->lwrite(' MailWizz: Failed looking up record ' . $recipient . ' with status=' . $response->body['status']);
$unsubscriberesponse = "Lookup failed with status=" . $response->body['status'];
} else if ($response->body['data']['count'] == 0) {
$log->lwrite(' MailWizz: Skipping ' . $recipient . ', already unsubscribed!');
$unsubscriberesponse = "Already unsubscribed!";
}
}

return $unsubscribeSuccess;
return array($unsubscribeSuccess, $unsubMessage);

}


// Lookup recipient via MailWizz X-Mw-Subscriber-Uid and List-Id
function MailWizz_getSubscriber($listID, $subscriberUID) {
global $log, $MailWizzEndPoint, $MAILWIZZ_HANDLER_ENABLED;

if ($MAILWIZZ_HANDLER_ENABLED == false) {
return array(false, null);
}

// Extract the list-id - sometimes we get "listid <some name>"
$mwListId = explode(" <", $listID, 2);

$response = $MailWizzEndPoint->getSubscriber($mwListId[0], $subscriberUID);

if ($response->body['status'] == "success" && !is_null($response->body['data']['record']['EMAIL']) && !empty($response->body['data']['record']['EMAIL'])) {
return array(true, $response->body['data']['record']['EMAIL']);
} else {
return array(false, null);
}

return array(false, null);

}
27 changes: 21 additions & 6 deletions providers/feedback-loop-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ function feedbackLoopEvent($recipient, $feedbackLoopRecord) {
global $log;

$log->lwrite('FBL received from: ' . $feedbackLoopRecord[1] . ' for=' . $feedbackLoopRecord[8] . ' via ' . $feedbackLoopRecord[8]);

// Handle your unsubscribe processing here
//Transactional_unsubscribeRecipient($recipient, $feedbackLoopRecord);
MailWizz_unsubscribeRecipient($recipient);
Interspire_unsubscribeRecipient($recipient);

// We check if we have the MailWizz header "List-Id" and "X-Mw-Subscriber-Uid" in the FBL, then we change the recipient
if (array_key_exists(20, $feedbackLoopRecord) && !is_null($feedbackLoopRecord[20]) && !empty($feedbackLoopRecord[20]) &&
array_key_exists(21, $feedbackLoopRecord) && !is_null($feedbackLoopRecord[21]) && !empty($feedbackLoopRecord[21])) {
$subscriberEmail = MailWizz_getSubscriber($feedbackLoopRecord[20], $feedbackLoopRecord[21]);

if ($subscriberEmail[0] == true) {
$log->lwrite('*** FBL record provided list-id=' . $feedbackLoopRecord[20] . ' and subscriberid=' . $feedbackLoopRecord[21] . ", using=" . $subscriberEmail[1]);
$recipient = $subscriberEmail[1];
}
}

$unsub_mailwizz = MailWizz_unsubscribeRecipient($recipient);

$unsub_interspire = Interspire_unsubscribeRecipient($recipient);

$unsubstatus = "<span title='" . $unsub_mailwizz[1] . "'>MailWizz=" . ($unsub_mailwizz[0] == true ? "OK":"Check") . "</span>"
. ", <span title='" . $unsub_interspire[1] . "'>Interspire=" . ($unsub_interspire[0] == true ? "OK":"Check") . "</span>";

// Send the email using PHPMailer
$mail = new PHPMailer();
Expand All @@ -70,14 +83,16 @@ function feedbackLoopEvent($recipient, $feedbackLoopRecord) {
$mail->Body = '<h2>Port25 - Feedback Loop</h2>'
. '<p>Port25 processed the following feedback loop complaint and the user was unsubscribed:</p>'
. '<table rules="all" style="border-color: #666;border: 1px;width: 50%;" cellpadding="10">'
. '<tr style="background: #eee"><td style="width:20%"><strong>Reporter:</strong> </td><td style="width:70%"><strong>' . $feedbackLoopRecord[8] . '</strong></td></tr>'
. '<tr style="background: #eee"><td style="width:20%"><strong>Reporter:</strong> </td><td style="width:70%"><strong>' . $recipient . '</strong>'
. (strcmp($recipient, $feedbackLoopRecord[8]) !== 0 ? " / " . $feedbackLoopRecord[8] : "") . '</td></tr>'
. '<tr><td><strong>Received:</strong> </td><td>' . $feedbackLoopRecord[1] . '</td></tr>'
. '<tr><td><strong>Reported via:</strong> </td><td>' . $feedbackLoopRecord[5] . '</td></tr>'
. '<tr><td><strong>Reported IP:</strong> </td><td>' . $feedbackLoopRecord[2] . '</td></tr>'
. '<tr><td><strong>Sender:</strong> </td><td>' . $feedbackLoopRecord[7] . '</td></tr>'
. '<tr><td><strong>Feedback ID:</strong> </td><td>' . $feedbackLoopRecord[14] . '</td></tr>'
. '<tr><td><strong>Subject:</strong> </td><td>' . $feedbackLoopRecord[15] . '</td></tr>'
. '<tr><td><strong>Message Id:</strong> </td><td><pre>' . rtrim(ltrim($feedbackLoopRecord[18], '<'), '>') . '</pre></td></tr>'
. '<tr><td><strong>Unsubscribe status:</strong> </td><td><pre>' . $unsubstatus . '</pre></td></tr>'
. '<tr><td><strong>List unsubscribe:</strong> </td><td>' . rtrim(ltrim($feedbackLoopRecord[19], '<'), '>') . '</td></tr>'
. '</table>';

Expand Down

0 comments on commit 548fafe

Please sign in to comment.