Skip to content

Commit

Permalink
Merge pull request mautic#2346 from Webmecanik/Mailjet_integration
Browse files Browse the repository at this point in the history
Mailjet integration
  • Loading branch information
escopecz authored Nov 25, 2016
2 parents 19cfe7e + 136efb9 commit bc91aa2
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/bundles/EmailBundle/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@
'setMauticFactory' => ['mautic.factory'],
],
],
'mautic.transport.mailjet' => [
'class' => 'Mautic\EmailBundle\Swiftmailer\Transport\MailjetTransport',
'serviceAlias' => 'swiftmailer.mailer.transport.%s',
'methodCalls' => [
'setUsername' => ['%mautic.mailer_user%'],
'setPassword' => ['%mautic.mailer_password%'],
],
],
'mautic.transport.sendgrid' => [
'class' => 'Mautic\EmailBundle\Swiftmailer\Transport\SendgridTransport',
'serviceAlias' => 'swiftmailer.mailer.transport.%s',
Expand Down
3 changes: 3 additions & 0 deletions app/bundles/EmailBundle/Form/Type/ConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'mautic.transport.elasticemail' => 'mautic.email.config.mailer_transport.elasticemail',
'gmail' => 'mautic.email.config.mailer_transport.gmail',
'mautic.transport.mandrill' => 'mautic.email.config.mailer_transport.mandrill',
'mautic.transport.mailjet' => 'mautic.email.config.mailer_transport.mailjet',
'smtp' => 'mautic.email.config.mailer_transport.smtp',
'mail' => 'mautic.email.config.mailer_transport.mail',
'mautic.transport.postmark' => 'mautic.email.config.mailer_transport.postmark',
Expand Down Expand Up @@ -343,6 +344,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
"cram-md5"
], "config_emailconfig_mailer_transport":[
"mautic.transport.mandrill",
"mautic.transport.mailjet",
"mautic.transport.sendgrid",
"mautic.transport.elasticemail",
"mautic.transport.amazon",
Expand All @@ -361,6 +363,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
"mautic.transport.sendgrid",
"mautic.transport.amazon",
"mautic.transport.postmark",
"mautic.transport.mailjet",
"gmail"
]
}';
Expand Down
119 changes: 119 additions & 0 deletions app/bundles/EmailBundle/Swiftmailer/Transport/MailjetTransport.php
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;
}
}
1 change: 1 addition & 0 deletions app/bundles/EmailBundle/Translations/en_US/messages.ini
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mautic.email.config.mailer_transport.amazon="Amazon SES"
mautic.email.config.mailer_transport.gmail="Gmail"
mautic.email.config.mailer_transport.mail="PHP Mail"
mautic.email.config.mailer_transport.mandrill="Mandrill"
mautic.email.config.mailer_transport.mailjet="Mailjet"
mautic.email.config.mailer_transport.postmark="Postmark"
mautic.email.config.mailer_transport.sendgrid="Sendgrid"
mautic.email.config.mailer_transport.elasticemail="Elastic Email"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'choices' => [
'mail' => 'mautic.email.config.mailer_transport.mail',
'mautic.transport.mandrill' => 'mautic.email.config.mailer_transport.mandrill',
'mautic.transport.mailjet' => 'mautic.email.config.mailer_transport.mailjet',
'mautic.transport.sendgrid' => 'mautic.email.config.mailer_transport.sendgrid',
'mautic.transport.amazon' => 'mautic.email.config.mailer_transport.amazon',
'mautic.transport.postmark' => 'mautic.email.config.mailer_transport.postmark',
Expand Down

0 comments on commit bc91aa2

Please sign in to comment.