forked from mautic/mautic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mautic#2346 from Webmecanik/Mailjet_integration
Mailjet integration
- Loading branch information
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
app/bundles/EmailBundle/Swiftmailer/Transport/MailjetTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright 2014 Mautic Contributors. All rights reserved | ||
* @author Mautic | ||
* | ||
* @link http://mautic.org | ||
* | ||
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
namespace Mautic\EmailBundle\Swiftmailer\Transport; | ||
|
||
use Mautic\CoreBundle\Factory\MauticFactory; | ||
use Mautic\LeadBundle\Entity\DoNotContact; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Class MailjetlTransport. | ||
*/ | ||
class MailjetTransport extends \Swift_SmtpTransport implements InterfaceCallbackTransport | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct($host = 'localhost', $port = 25, $security = null) | ||
{ | ||
parent::__construct('in-v3.mailjet.com', 587, 'tls'); | ||
$this->setAuthMode('login'); | ||
} | ||
|
||
public function send(\Swift_Mime_Message $message, &$failedRecipients = null) | ||
{ | ||
|
||
// add leadIdHash to track this email | ||
if (isset($message->leadIdHash)) { | ||
// contact leadidHeash and email to be sure not applying email stat to bcc | ||
$message->getHeaders()->addTextHeader('X-MJ-CUSTOMID', $message->leadIdHash.'-'.key($message->getTo())); | ||
} | ||
|
||
parent::send($message, $failedRecipients); | ||
} | ||
|
||
/** | ||
* Returns a "transport" string to match the URL path /mailer/{transport}/callback. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getCallbackPath() | ||
{ | ||
return 'mailjet'; | ||
} | ||
|
||
/** | ||
* Handle response. | ||
* | ||
* @param Request $request | ||
* @param MauticFactory $factory | ||
* | ||
* @return mixed | ||
*/ | ||
public function handleCallbackResponse(Request $request, MauticFactory $factory) | ||
{ | ||
$postData = json_decode($request->getContent(), true); | ||
$rows = [ | ||
DoNotContact::BOUNCED => [ | ||
'hashIds' => [], | ||
'emails' => [], | ||
], | ||
DoNotContact::UNSUBSCRIBED => [ | ||
'hashIds' => [], | ||
'emails' => [], | ||
], | ||
]; | ||
|
||
if (is_array($postData) && isset($postData['event'])) { | ||
// Mailjet API callback V1 | ||
$events = [ | ||
$postData, | ||
]; | ||
} elseif (is_array($postData)) { | ||
// Mailjet API callback V2 | ||
$events = $postData; | ||
} else { | ||
// respone must be an array | ||
return null; | ||
} | ||
|
||
foreach ($events as $event) { | ||
if (in_array($event['event'], [ | ||
'bounce', | ||
'blocked', | ||
'spam', | ||
'unsub', | ||
])) { | ||
if ($event['event'] === 'bounce' || $event['event'] === 'blocked') { | ||
$reason = $event['error_related_to'].' : '.$event['error']; | ||
$type = DoNotContact::BOUNCED; | ||
} elseif ($event['event'] === 'spam') { | ||
$reason = 'User reported email as spam, source :'.$event['source']; | ||
$type = DoNotContact::BOUNCED; | ||
} elseif ($event['event'] === 'unsub') { | ||
$reason = 'User unsubscribed'; | ||
$type = DoNotContact::UNSUBSCRIBED; | ||
} | ||
if (isset($event['CustomID']) && $event['CustomID'] !== '' && strpos($event['CustomID'], '-', 0) !== false) { | ||
list($leadIdHash, $leadEmail) = explode('-', $event['CustomID']); | ||
if ($event['email'] == $leadEmail) { | ||
$rows[$type]['hashIds'][$leadIdHash] = $reason; | ||
} | ||
} else { | ||
$rows[$type]['emails'][$event['email']] = $reason; | ||
} | ||
} | ||
} | ||
|
||
return $rows; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters