Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RemoteEvent][Webhook] Add Mailjet support #50502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailjet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add `RemoteEvent` and `Webhook` support

6.3
---

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailjet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ where:
- `ACCESS_KEY` is your Mailjet access key
- `SECRET_KEY` is your Mailjet secret key

Webhook
-------

When you [setup your webhook URL](https://app.mailjet.com/account/triggers) on Mailjet you must not group events by unchecking the checkboxes.

Resources
---------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Mailjet\RemoteEvent;

use Symfony\Component\RemoteEvent\Event\Mailer\AbstractMailerEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
use Symfony\Component\RemoteEvent\Exception\ParseException;
use Symfony\Component\RemoteEvent\PayloadConverterInterface;

final class MailjetPayloadConverter implements PayloadConverterInterface
{
public function convert(array $payload): AbstractMailerEvent
{
if (\in_array($payload['event'], ['bounce', 'sent', 'blocked'], true)) {
$name = match ($payload['event']) {
'bounce' => MailerDeliveryEvent::BOUNCE,
'sent' => MailerDeliveryEvent::DELIVERED,
'blocked' => MailerDeliveryEvent::DROPPED,
};

$event = new MailerDeliveryEvent($name, $payload['MessageID'], $payload);
$event->setReason($this->getReason($payload));
} else {
$name = match ($payload['event']) {
'click' => MailerEngagementEvent::CLICK,
'open' => MailerEngagementEvent::OPEN,
'spam' => MailerEngagementEvent::SPAM,
'unsub' => MailerEngagementEvent::UNSUBSCRIBE,
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['event'])),
};
$event = new MailerEngagementEvent($name, $payload['MessageID'], $payload);
}

if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['time'])) {
throw new ParseException(sprintf('Invalid date "%s".', $payload['time']));
}

$event->setDate($date);
$event->setRecipientEmail($payload['email']);

if (isset($payload['CustomID'])) {
$event->setTags([$payload['CustomID']]);
}

if (isset($payload['Payload'])) {
$event->setMetadata(['Payload' => $payload['Payload']]);
}

return $event;
}

private function getReason(array $payload): string
{
return $payload['smtp_reply'] ?? $payload['error_related_to'] ?? '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"event": "blocked",
"time": 1430812195,
"MessageID": 13792286917004336,
"Message_GUID": "1ab23cd4-e567-8901-2345-6789f0gh1i2j",
"email": "[email protected]",
"mj_campaign_id": 0,
"mj_contact_id": 1000,
"customcampaign": "",
"CustomID": "helloworld",
"Payload": "",
"error_related_to": "mailjet",
"error": "preblocked"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DROPPED, '13792286917004336', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR));
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1430812195));
$wh->setReason('mailjet');
$wh->setTags(['helloworld']);
$wh->setMetadata(['Payload' => '']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"event": "bounce",
"time": 1685525050,
"MessageID": 104427216766056450,
"Message_GUID": "5577705c-024a-472d-8918-29ef81a64738",
"email": "[email protected]",
"mj_campaign_id": 0,
"mj_contact_id": 1000,
"customcampaign": "",
"blocked": false,
"hard_bounce": false,
"error_related_to": "policy issue",
"error": "",
"comment": "421 4.7.0 [TSS04] Messages from 87.253.233.123 temporarily deferred due to unexpected volume or user complaints - 4.16.55.1; see https://postmaster.yahooinc.com/error-codes",
"CustomID": "helloworld",
"Payload": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::BOUNCE, '104427216766056450', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR));
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1685525050));
$wh->setReason('policy issue');
$wh->setTags(['helloworld']);
$wh->setMetadata(['Payload' => '']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"event": "click",
"time": 1685519224,
"MessageID": 93449692684977140,
"Message_GUID": "245e4120-9d53-41b7-91f5-9aac8fda3cb0",
"email": "[email protected]",
"mj_campaign_id": 0,
"mj_contact_id": 1000,
"customcampaign": "",
"url": "https://mailjet.com",
"ip": "127.0.0.1",
"geo": "FR",
"agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 CCleaner/113.0.21244.129",
"CustomID": "helloworld",
"Payload": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;

$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '93449692684977140', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR));
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1685519224));
$wh->setTags(['helloworld']);
$wh->setMetadata(['Payload' => '']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"event": "open",
"time": 1685519055,
"MessageID": 102175416994919440,
"Message_GUID": "982f91f1-4417-4ab1-9777-a2410e1bde36",
"email": "[email protected]",
"mj_campaign_id": 0,
"mj_contact_id": 1000,
"customcampaign": "",
"ip": "127.0.0.1",
"geo": "EU",
"agent": "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko Firefox/11.0 (via ggpht.com GoogleImageProxy)",
"CustomID": "helloworld",
"Payload": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;

$wh = new MailerEngagementEvent(MailerEngagementEvent::OPEN, '102175416994919440', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR));
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1685519055));
$wh->setTags(['helloworld']);
$wh->setMetadata(['Payload' => '']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"event": "sent",
"time": 1685518742,
"MessageID": 92042317804662640,
"Message_GUID": "5b4de5f5-63d0-44f3-b4bd-a34f222cb8af",
"email": "[email protected]",
"mj_campaign_id": 0,
"mj_contact_id": 1000,
"customcampaign": "",
"CustomID": "helloworld",
"Payload": "",
"smtp_reply": "250 2.0.0 OK 1685518742 k22-20020a05600c0b5600b003f6020d9976si8376621wmr.181 - gsmtp"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DELIVERED, '92042317804662640', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR));
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1685518742));
$wh->setReason('250 2.0.0 OK 1685518742 k22-20020a05600c0b5600b003f6020d9976si8376621wmr.181 - gsmtp');
$wh->setTags(['helloworld']);
$wh->setMetadata(['Payload' => '']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"event": "spam",
"time": 1430812195,
"MessageID": 13792286917004336,
"Message_GUID": "1ab23cd4-e567-8901-2345-6789f0gh1i2j",
"email": "[email protected]",
"mj_campaign_id": 0,
"mj_contact_id": 1000,
"customcampaign": "",
"CustomID": "helloworld",
"Payload": "",
"source": "JMRPP"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;

$wh = new MailerEngagementEvent(MailerEngagementEvent::SPAM, '13792286917004336', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR));
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1430812195));
$wh->setTags(['helloworld']);
$wh->setMetadata(['Payload' => '']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"event": "unsub",
"time": 1433334941,
"MessageID": 20547674933128000,
"Message_GUID": "1ab23cd4-e567-8901-2345-6789f0gh1i2j",
"email": "[email protected]",
"mj_campaign_id": 0,
"mj_contact_id": 1000,
"customcampaign": "",
"CustomID": "helloworld",
"Payload": "",
"mj_list_id": 1,
"ip": "127.0.0.1",
"geo": "FR",
"agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;

$wh = new MailerEngagementEvent(MailerEngagementEvent::UNSUBSCRIBE, '20547674933128000', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR));
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1433334941));
$wh->setTags(['helloworld']);
$wh->setMetadata(['Payload' => '']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Mailjet\Tests\Webhook;

use Symfony\Component\Mailer\Bridge\Mailjet\RemoteEvent\MailjetPayloadConverter;
use Symfony\Component\Mailer\Bridge\Mailjet\Webhook\MailjetRequestParser;
use Symfony\Component\Webhook\Client\RequestParserInterface;
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase;

class MailjetRequestParserTest extends AbstractRequestParserTestCase
{
protected function createRequestParser(): RequestParserInterface
{
return new MailjetRequestParser(new MailjetPayloadConverter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Mailjet\Webhook;

use Symfony\Component\HttpFoundation\ChainRequestMatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher\IsJsonRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\Mailer\Bridge\Mailjet\RemoteEvent\MailjetPayloadConverter;
use Symfony\Component\RemoteEvent\Event\Mailer\AbstractMailerEvent;
use Symfony\Component\RemoteEvent\Exception\ParseException;
use Symfony\Component\Webhook\Client\AbstractRequestParser;
use Symfony\Component\Webhook\Exception\RejectWebhookException;

final class MailjetRequestParser extends AbstractRequestParser
{
public function __construct(
private readonly MailjetPayloadConverter $converter,
) {
}

protected function getRequestMatcher(): RequestMatcherInterface
{
return new ChainRequestMatcher([
new MethodRequestMatcher('POST'),
new IsJsonRequestMatcher(),
]);
}

protected function doParse(Request $request, string $secret): ?AbstractMailerEvent
{
try {
return $this->converter->convert($request->toArray());
} catch (ParseException $e) {
throw new RejectWebhookException(406, $e->getMessage(), $e);
}
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/Mailer/Bridge/Mailjet/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"symfony/mailer": "^5.4.21|^6.2.7|^7.0"
},
"require-dev": {
"symfony/http-client": "^5.4|^6.0|^7.0"
"symfony/http-client": "^5.4|^6.0|^7.0",
"symfony/webhook": "^6.4"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Mailjet\\": "" },
Expand Down