Skip to content

Commit

Permalink
Introduce SparkPost as an inbound email provider.
Browse files Browse the repository at this point in the history
See #77.
  • Loading branch information
r-a-y committed Feb 27, 2016
1 parent 8750c3f commit 4d9c4ad
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bp-rbe-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ protected function load_inbound_provider() {
*/
public static function get_inbound_providers() {
$default = array(
'postmark' => 'BP_Reply_By_Email_Inbound_Provider_Postmark',
'mandrill' => 'BP_Reply_By_Email_Inbound_Provider_Mandrill'
'postmark' => 'BP_Reply_By_Email_Inbound_Provider_Postmark',
'sparkpost' => 'BP_Reply_By_Email_Inbound_Provider_Sparkpost',
'mandrill' => 'BP_Reply_By_Email_Inbound_Provider_Mandrill'
);

// If you've added a custom inbound provider, register it with this filter
Expand Down
1 change: 1 addition & 0 deletions includes/bp-rbe-classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
require dirname( __FILE__ ) . '/classes/bp-reply-by-email-imap.php';
require dirname( __FILE__ ) . '/classes/bp-reply-by-email-inbound-provider.php';
require dirname( __FILE__ ) . '/classes/bp-reply-by-email-inbound-provider-postmark.php';
require dirname( __FILE__ ) . '/classes/bp-reply-by-email-inbound-provider-sparkpost.php';
require dirname( __FILE__ ) . '/classes/bp-reply-by-email-inbound-provider-mandrill.php';
require dirname( __FILE__ ) . '/classes/bp-reply-by-email-parser.php';
84 changes: 84 additions & 0 deletions includes/classes/bp-reply-by-email-inbound-provider-sparkpost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* BP Reply By Email SparkPost Inbound Provider Class.
*
* @package BP_Reply_By_Email
* @subpackage Classes
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

/**
* Add SparkPost as an inbound provider for RBE.
*
* @since 1.0-RC4
*
* @see https://support.sparkpost.com/customer/portal/articles/2039614-enabling-inbound-email-relaying-relay-webhooks
*/
class BP_Reply_By_Email_Inbound_Provider_Sparkpost extends BP_Reply_By_Email_Inbound_Provider {
/**
* @var string Display name for our inbound provider
*/
public static $name = 'SparkPost';

/**
* Webhook parser class method for SparkPost.
*/
public function webhook_parser() {
if ( empty( $_SERVER['CONTENT_TYPE'] ) || ( ! empty( $_SERVER['CONTENT_TYPE'] ) && 'application/json' !== $_SERVER['CONTENT_TYPE'] ) ) {
return;
}

if ( false === isset( $_SERVER['HTTP_X_MESSAGESYSTEMS_WEBHOOK_TOKEN'] ) ) {
return;
}

bp_rbe_log( '- SparkPost webhook received -' );

// SparkPost auth token verification.
if ( defined( 'BP_RBE_SPARKPOST_WEBHOOK_TOKEN' ) && ! empty( $_SERVER['HTTP_X_MESSAGESYSTEMS_WEBHOOK_TOKEN'] ) ) {
// Make sure auth token matches; if it doesn't, bail!
if ( constant( 'BP_RBE_SPARKPOST_WEBHOOK_TOKEN' ) !== $_SERVER['HTTP_X_MESSAGESYSTEMS_WEBHOOK_TOKEN'] ) {
bp_rbe_log( 'SparkPost token verification failed.' );
die();
}
}

$response = file_get_contents( 'php://input' );
if ( empty( $response ) ) {
bp_rbe_log( '- SporkPost webhook response failed -' );
}

$response = json_decode( $response );

$i = 1;
foreach ( $response as $item ) {
// Format email headers to fit RBE spec.
$headers = array();
foreach ( $item->msys->relay_message->content->headers as $header ) {
$headers = array_merge( $headers, (array) $header );
}

$data = array(
'headers' => $headers,
'to_email' => $item->msys->relay_message->rcpt_to,
'from_email' => $item->msys->relay_message->friendly_from,
'content' => $item->msys->relay_message->content->text,
'subject' => $item->msys->relay_message->content->subject
);

$parser = BP_Reply_By_Email_Parser::init( $data, $i );

if ( is_wp_error( $parser ) ) {
do_action( 'bp_rbe_no_match', $parser, $data, $i, false );
}

++$i;
}

bp_rbe_log( '- Webhook parsing completed -' );

die();
}
}

0 comments on commit 4d9c4ad

Please sign in to comment.