Skip to content

Commit

Permalink
FIX: Updated mandrill web hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mellisa Hankins committed Jun 4, 2015
1 parent 76742dc commit acc2ad2
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 113 deletions.
3 changes: 3 additions & 0 deletions _config/listeners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Eventful:
'sendthis:blacklisted.sendthis:default':
- 'Milkyway\SS\SendThis\Listeners\Logging'
- 'Milkyway\SS\SendThis\Listeners\Mandrill\Logging'
'sendthis:unsubscribed.sendthis:default':
- 'Milkyway\SS\SendThis\Listeners\Logging'
- 'Milkyway\SS\SendThis\Listeners\Mandrill\Logging'
'sendthis:bounced.sendthis:default':
- 'Milkyway\SS\SendThis\Listeners\Logging'
- 'Milkyway\SS\SendThis\Listeners\Mandrill\Logging'
Expand Down
2 changes: 1 addition & 1 deletion _config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SendThis:
mandrill:
class: 'Milkyway\SS\SendThis\Transports\Mandrill'

logging: true
logging: false
api_tracking: true
from_same_domain_only: true
blacklist_after_bounced: 2
20 changes: 20 additions & 0 deletions code/Controllers/Mandrill.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Mandrill extends \Controller
'blacklist' => 'blacklisted',
];

protected $syncEvents = [
'blacklist',
'whitelist',
];

public function index($request)
{
if ($request->isHEAD()) {
Expand Down Expand Up @@ -81,6 +86,10 @@ protected function getTemplates($action = '') {
protected function handleEvent($eventParams, $request = null)
{
$event = isset($eventParams['event']) ? $eventParams['event'] : 'unknown';

if(isset($eventParams['type']) && in_array($eventParams['type'], $this->syncEvents))
$event = $eventParams['type'];

$messageId = '';
$email = '';

Expand All @@ -89,6 +98,13 @@ protected function handleEvent($eventParams, $request = null)
'timestamp' => isset($eventParams['ts']) ? $eventParams['ts'] : '',
];

if(!isset($eventParams['msg'])) {
if(isset($eventParams['reject']))
$params['details'] = $eventParams['reject'];
else if(isset($eventParams['entry']))
$params['details'] = $eventParams['entry'];
}

if (count($params['details'])) {
if (isset($params['details']['_id'])) {
$messageId = $params['details']['_id'];
Expand All @@ -107,6 +123,10 @@ protected function handleEvent($eventParams, $request = null)
$params['permanent'] = true;
}

if($event == 'blacklist' && isset($eventParams['action']) && $eventParams['action'] != 'add') {
$event = 'whitelist';
}

if (isset($this->eventMapping[$event]))
$event = $this->eventMapping[$event];

Expand Down
47 changes: 36 additions & 11 deletions code/listeners/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ public function failed($e, $messageId = '', $email = '', $params = [], $response
}

public function bounced(Event $e, $messageId = '', $email = '', $params = [], $response = []) {
if (!$this->allowed($e->mailer())) return;

$base = '@' . trim(Director::baseWebsiteURL(), ' /');
$blacklistAfter = $e->mailer()->config()->blacklist_after_bounced ?: 2;
$permanent = isset($params['permanent']);
$permanent = isset($params['permanent']) && $params['permanent'];

$bounce = null;

Expand All @@ -100,15 +102,24 @@ public function bounced(Event $e, $messageId = '', $email = '', $params = [], $r

$params['message'] .= "\n\n" . print_r($response, true);

if(!(substr($email, -strlen($base)) === $base)) {
if((substr($email, -strlen($base)) !== $base)) {
$bounce = \SendThis_Bounce::create();
$bounce->Email = $email;
$bounce->Message = $params['message'];

if ((\SendThis_Bounce::get()->filter('Email', $email)->count() + 1) >= $blacklistAfter || $permanent) {
$message = "\n\n" . print_r($response, true);
$message = $permanent ? 'Permanent Bounce' . $message : 'Bounced too many times' . $message;
$e->mailer()->eventful()->fire(Event::named('spam', $e->mailer()), $messageId, $email, $params + ['message' => $message], $response);
$message = $permanent ? 'Permanent Bounce. ' . $message : 'Bounced too many times. ' . $message;

$e->mailer()->eventful()->fire(
Event::named('sendthis:spam', $e->mailer()),
$messageId,
$email,
array_merge($params, ['message' => $message, 'valid_email' => false]),
$response
);

$bounce->Message = $message;
$bounce->write();
return;
}
Expand All @@ -125,12 +136,16 @@ public function spam($e, $messageId = '', $email = '', $params = [], $response =
if(!isset($params['message']))
$params['message'] = 'The email address marked this email as spam';

if(!array_key_exists('valid_email', $params))
$params['valid_email'] = true;

$this->updateBadEmail($messageId, $email, $params);
}

public function rejected($e, $messageId = '', $email = '', $params = [], $response = []) {
if(!isset($params['message']))
$params['message'] = 'The end point has rejected this email for some reason';
if(!isset($params['message'])) {
$params['message'] = 'The end point has rejected this email for some reason. Usually this is because the recent bounce for this recipient, the recipient has registered a spam complaint, the recipient is unsubscribed from emails from your application, or the recipient has been blacklisted.';
}

$this->updateBadEmail($messageId, $email, $params);
}
Expand All @@ -139,16 +154,26 @@ public function blacklisted($e, $messageId = '', $email = '', $params = [], $res
if(!isset($params['message']))
$params['message'] = 'The user of this email has requested to be blacklisted from this application';

$params['valid_email'] = true;
if(!array_key_exists('valid_email', $params))
$params['valid_email'] = true;

$this->updateBadEmail($messageId, $email, $params);
}

public function unsubscribed($e, $messageId = '', $email = '', $params = [], $response = []) {
if(!isset($params['message']))
$params['message'] = 'The user of this email has requested to be unsubscribed from this application';

$params['valid_email'] = true;

$this->updateBadEmail('', $email, $params);
}

public function whitelisted($e, $messageId = '', $email = '', $params = [], $response = []) {
if(!$email) return;

if(!isset($params['message']))
$params['message'] = 'The user of this email has requested to be whitelisted for this application';
// if(!isset($params['message']))
// $params['message'] = 'The user of this email has requested to be whitelisted for this application';

$blocked = \SendThis_Blacklist::get()->filter(['Email' => $email, 'Valid' => true]);

Expand All @@ -163,7 +188,7 @@ public function whitelisted($e, $messageId = '', $email = '', $params = [], $res
protected function updateBadEmail($messageId = '', $email = '', $params = array()) {
if($email) {
$message = isset($params['message']) ? $params['message'] : 'Unknown';
$this->blacklistEmail($email, $message, isset($params['valid_email']));
$this->blacklistEmail($email, $message, (isset($params['valid_email']) && $params['valid_email']));
}

if($messageId)
Expand All @@ -183,7 +208,7 @@ protected function updateLogsForMessageId($messageId, $params, $bounce = null) {
}
}

protected function updateLog($log, $params = array(), $bounce = null) {
protected function updateLog($log, $params = [], $bounce = null) {
$log->Success = isset($params['success']);

if(isset($params['MessageID']))
Expand Down
4 changes: 1 addition & 3 deletions code/listeners/mandrill/Logging.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php namespace Milkyway\SS\SendThis\Listeners\Mandrill;
use Milkyway\SS\SendThis\Mailer;
use Milkyway\SS\SendThis\Transports\Mandrill;

/**
* Milkyway Multimedia
Expand All @@ -12,6 +10,6 @@

class Logging extends \Milkyway\SS\SendThis\Listeners\Logging {
protected function allowed(\Object $mailer) {
return !parent::allowed($mailer) && $mailer->config()->api_tracking && ($mailer instanceof Mailer) && ($mailer->transport() instanceof Mandrill);
return !parent::allowed($mailer) && $mailer->config()->api_tracking;
}
}
Loading

0 comments on commit acc2ad2

Please sign in to comment.