Skip to content

Instantly share code, notes, and snippets.

@bhwebworks
Last active April 25, 2023 19:26
Show Gist options
  • Save bhwebworks/5b5f8db76b84729b56572623a607a0b0 to your computer and use it in GitHub Desktop.
Save bhwebworks/5b5f8db76b84729b56572623a607a0b0 to your computer and use it in GitHub Desktop.
Automatic bounce and complaint management using Postmark to send MailPoet (and other) emails
add_action( 'admin_post_nopriv_process_postmark_events', 'bhww_core_postmark_events_webhook' );
/**
* Add a webhook to enable automated MailPoet bounce (or spam complaint or unsubscribe)
* handling from Postmark events JSON data
*
* Change this webhook URL for each site, and copy/paste into Postmark Webhook URL form:
* https://[YOUR WEBSITE URL]/wp-admin/admin-post.php?action=process_postmark_events
*
* Make sure there is not an admin redirect active,
* or if there is, modify it to allow webhook access
*
* Give credit where credit is due:
* @link https://bhanusingh.in/creating-a-custom-webhook-in-wordpress-to-get-data-from-third-part-services
* @link https://developer.wordpress.org/reference/hooks/admin_post_nopriv_action/
*
* @since 2022/12/12
*/
function bhww_core_postmark_events_webhook() {
$email = '';
$type = '';
// Get data from the webhoook
$request = file_get_contents( 'php://input' );
// Decode the JSON data
$json_data = json_decode( $request, true );
if ( ! empty( $json_data ) ) {
if ( ! empty( $json_data['Email'] ) ) {
$email = $json_data['Email'];
} else {
$email = $json_data['Recipient']; // Subscription Change
}
if ( ! empty( $json_data['Type'] ) ) {
$type = $json_data['Type'];
} else {
$type = $json_data['RecordType']; // Subscription Change
}
}
// log it in /wp-content/debug.log
// WP_DEBUG and WP_DEBUG_LOG need to be set to 'true' in wp-config.php for this to work
error_log( print_r( $json_data, true ) );
// Notification email information
$site = '[YOUR WEBSITE URL]'; // Change to match website URL
$to = '[YOUR EMAIL ADDRESS]'; // This is where the email will be sent
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
// These event types come from Postmark
$event_types = array( 'HardBounce', 'SpamComplaint', 'SubscriptionChange' );
if ( in_array( $type, $event_types ) ) {
if ( $type === 'HardBounce' ) {
error_log( 'This email bounced' );
$status = 'bounced';
$subject = 'Postmark bounce - ' . $site;
$body = 'The email address ' . $email . ' just bounced on ' . $site;
} elseif ( $type === 'SpamComplaint' ) {
error_log( 'This user marked us as spam!!!' );
$status = 'unsubscribed';
$subject = 'Postmark spam complaint - ' . $site;
$body = 'The email address ' . $email . ' just marked us as spam on ' . $site;
} elseif ( $type === 'SubscriptionChange' ) {
error_log( 'This user unsubscribed' );
$status = 'unsubscribed';
$subject = 'Postmark unsubscribe - ' . $site;
$body = 'The email address ' . $email . ' just unsubscribed on ' . $site;
}
// Send an email to yourself about this event
wp_mail( $to, $subject, $body, $headers );
// Check to see if MailPoet plugin is active
if ( is_plugin_active( 'mailpoet/mailpoet.php' ) ) {
// Update the subscriber in MailPoet
global $wpdb; // Need this!
$table = $wpdb->prefix . 'mailpoet_subscribers';
// The new data value
$data = array(
'status' => $status,
);
// If these conditions are met
$where = array(
'email' => $email,
'status' => 'subscribed',
);
// Update the subscriber's status in the database
$wpdb->update( $table, $data, $where );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment