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

Skip to content
Closed
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/Sendgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add support for `RemoteEvent` and `Webhook`

5.4
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Sendgrid\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 SendgridPayloadConverter implements PayloadConverterInterface
{
public function convert(array $payload): AbstractMailerEvent
{
if (\in_array($payload['event'], ['bounce', 'deferred', 'delivered', 'dropped', 'processed'], true)) {
$name = match ($payload['event']) {
'bounce' => MailerDeliveryEvent::BOUNCE,
'deferred' => MailerDeliveryEvent::DEFERRED,
'delivered' => MailerDeliveryEvent::DELIVERED,
'dropped' => MailerDeliveryEvent::DROPPED,
'processed' => MailerDeliveryEvent::RECEIVED,
};
$event = new MailerDeliveryEvent($name, $payload['sg_event_id'], $payload);
if (isset($payload['reason'])) {
$event->setReason($payload['reason']);
}
} else {
$name = match ($payload['event']) {
'click' => MailerEngagementEvent::CLICK,
'group_unsubscribe' => MailerEngagementEvent::UNSUBSCRIBE,
'open' => MailerEngagementEvent::OPEN,
'spamreport' => MailerEngagementEvent::SPAM,
'unsubscribe' => MailerEngagementEvent::UNSUBSCRIBE,
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['event'])),
};
$event = new MailerEngagementEvent($name, $payload['sg_event_id'], $payload);
}

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

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

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

return $event;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"bounce_classification": "invalid",
"event": "bounce",
"ip": "168.1.1.1",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id",
"reason": "500 unknown recipient",
"status": "5.0.0"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::BOUNCE, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setReason('500 unknown recipient');
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "click",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id",
"useragent": "Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"ip": "255.255.255.255",
"url": "http://www.sendgrid.com/"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "deferred",
"ip": "168.1.1.1",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id",
"response": "400 try again later",
"attempt": "5"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DEFERRED, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "delivered",
"ip": "168.1.1.1",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id",
"response": "250 OK"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DELIVERED, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "dropped",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id",
"reason": "Bounced Address",
"status": "5.0.0"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DROPPED, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setReason('Bounced Address');
$wh->setRecipientEmail('[email protected]');
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "group_unsubscribe",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id",
"useragent": "Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"ip": "255.255.255.255",
"url": "http://www.sendgrid.com/",
"asm_group_id": 10
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$wh = new MailerEngagementEvent(MailerEngagementEvent::UNSUBSCRIBE, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "open",
"sg_machine_open": false,
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id",
"useragent": "Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"ip": "255.255.255.255"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$wh = new MailerEngagementEvent(MailerEngagementEvent::OPEN, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "processed",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$wh = new MailerDeliveryEvent(MailerDeliveryEvent::RECEIVED, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setRecipientEmail('[email protected]');
$wh->setTags(['cat facts']);

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "spamreport",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

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

$wh = new MailerEngagementEvent(MailerEngagementEvent::SPAM, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setRecipientEmail('[email protected]');
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"email": "[email protected]",
"timestamp": 1513299569,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "unsubscribe",
"category": "cat facts",
"sg_event_id": "sg_event_id",
"sg_message_id": "sg_message_id"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$wh = new MailerEngagementEvent(MailerEngagementEvent::UNSUBSCRIBE, 'sg_event_id', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: \JSON_THROW_ON_ERROR)[0]);
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1513299569));
$wh->setRecipientEmail('[email protected]');
$wh->setTags(['cat facts']);

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\Sendgrid\Tests\Webhook;

use Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent\SendgridPayloadConverter;
use Symfony\Component\Mailer\Bridge\Sendgrid\Webhook\SendgridRequestParser;
use Symfony\Component\Webhook\Client\RequestParserInterface;
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase;

class SendgridRequestParserTest extends AbstractRequestParserTestCase
{
protected function createRequestParser(): RequestParserInterface
{
return new SendgridRequestParser(new SendgridPayloadConverter());
}
}
Loading