-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,124 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
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,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.2 | ||
--- | ||
|
||
* Add the bridge |
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,19 @@ | ||
Copyright (c) 2024-present Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,71 @@ | ||
Mailomat Bridge | ||
=============== | ||
|
||
Provides [Mailomat](https://mailomat.swiss) integration for Symfony Mailer. | ||
|
||
Mailer | ||
------- | ||
|
||
Configuration example: | ||
|
||
```env | ||
# .env.local | ||
# SMTP | ||
MAILER_DSN=mailomat+smtp://USERNAME:PASSWORD@default | ||
# API | ||
MAILER_DSN=mailomat+api://KEY@default | ||
``` | ||
|
||
Where: | ||
- `USERNAME` is your Mailomat SMTP username (must use your full email address) | ||
- `PASSWORD` is your Mailomat SMTP password | ||
- `KEY` is your Mailomat API key | ||
|
||
|
||
Webhook | ||
------- | ||
|
||
Create a route: | ||
|
||
```yaml | ||
framework: | ||
webhook: | ||
routing: | ||
mailomat: | ||
service: mailer.webhook.request_parser.mailomat | ||
secret: '%env(WEBHOOK_MAILOMAT_SECRET)%' | ||
``` | ||
The configuration: | ||
```env | ||
# .env.local | ||
|
||
WEBHOOK_MAILOMAT_SECRET=your-mailomat-webhook-secret | ||
``` | ||
|
||
And a consumer: | ||
|
||
```php | ||
#[\Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer(name: 'mailomat')] | ||
class MailomatConsumer implements ConsumerInterface | ||
{ | ||
public function consume(AbstractMailerEvent $event): void | ||
{ | ||
// your code | ||
} | ||
} | ||
``` | ||
|
||
Where: | ||
- `WEBHOOK_MAILOMAT_SECRET` is your Mailomat Webhook secret | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/Mailer/Bridge/Mailomat/RemoteEvent/MailomatPayloadConverter.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,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Mailomat\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 MailomatPayloadConverter implements PayloadConverterInterface | ||
{ | ||
public function convert(array $payload): AbstractMailerEvent | ||
{ | ||
if (\in_array($payload['eventType'], ['accepted', 'not_accepted', 'delivered', 'failure_tmp', 'failure_perm'], true)) { | ||
$name = match ($payload['eventType']) { | ||
'accepted' => MailerDeliveryEvent::RECEIVED, | ||
'not_accepted' => MailerDeliveryEvent::DROPPED, | ||
'delivered' => MailerDeliveryEvent::DELIVERED, | ||
'failure_tmp' => MailerDeliveryEvent::DEFERRED, | ||
'failure_perm' => MailerDeliveryEvent::BOUNCE, | ||
}; | ||
$event = new MailerDeliveryEvent($name, $payload['id'], $payload); | ||
if (isset($payload['payload']['reason'])) { | ||
$event->setReason($payload['payload']['reason']); | ||
} | ||
} else { | ||
$name = match ($payload['eventType']) { | ||
'opened' => MailerEngagementEvent::OPEN, | ||
'clicked' => MailerEngagementEvent::CLICK, | ||
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['eventType'])), | ||
}; | ||
$event = new MailerEngagementEvent($name, $payload['id'], $payload); | ||
} | ||
|
||
if (!$date = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $payload['occurredAt'])) { | ||
throw new ParseException(sprintf('Invalid date "%s".', $payload['occurredAt'])); | ||
} | ||
|
||
$event->setDate($date); | ||
$event->setRecipientEmail($payload['recipient']); | ||
|
||
return $event; | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
src/Symfony/Component/Mailer/Bridge/Mailomat/Tests/Transport/MailomatApiTransportTest.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,147 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Mailomat\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\JsonMockResponse; | ||
use Symfony\Component\Mailer\Bridge\Mailomat\Transport\MailomatApiTransport; | ||
use Symfony\Component\Mailer\Exception\HttpTransportException; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class MailomatApiTransportTest extends TestCase | ||
{ | ||
private const KEY = 'K3Y'; | ||
|
||
/** | ||
* @dataProvider getTransportData | ||
*/ | ||
public function testToString(MailomatApiTransport $transport, string $expected): void | ||
{ | ||
$this->assertSame($expected, (string) $transport); | ||
} | ||
|
||
public static function getTransportData(): iterable | ||
{ | ||
yield [ | ||
new MailomatApiTransport(self::KEY), | ||
'mailomat+api://api.mailomat.swiss', | ||
]; | ||
|
||
yield [ | ||
(new MailomatApiTransport(self::KEY))->setHost('example.com'), | ||
'mailomat+api://example.com', | ||
]; | ||
|
||
yield [ | ||
(new MailomatApiTransport(self::KEY))->setHost('example.com')->setPort(99), | ||
'mailomat+api://example.com:99', | ||
]; | ||
} | ||
|
||
public function testSend() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
$this->assertSame('POST', $method); | ||
$this->assertSame('https://api.mailomat.swiss/message', $url); | ||
$this->assertContains('Authorization: Bearer '.self::KEY, $options['headers']); | ||
$this->assertContains('Content-Type: application/json', $options['headers']); | ||
$this->assertContains('Accept: application/json', $options['headers']); | ||
|
||
$body = json_decode($options['body'], true); | ||
$this->assertSame('from@mailomat.swiss', $body['from']['email']); | ||
$this->assertSame('From Doe', $body['from']['name']); | ||
|
||
$this->assertSame('to@mailomat.swiss', $body['to'][0]['email']); | ||
$this->assertSame('To Doe', $body['to'][0]['name']); | ||
$this->assertSame('to-simple@mailomat.swiss', $body['to'][1]['email']); | ||
|
||
$this->assertSame('cc@mailomat.swiss', $body['cc'][0]['email']); | ||
$this->assertSame('Cc Doe', $body['cc'][0]['name']); | ||
$this->assertSame('cc-simple@mailomat.swiss', $body['cc'][1]['email']); | ||
|
||
$this->assertSame('bcc@mailomat.swiss', $body['bcc'][0]['email']); | ||
$this->assertSame('Bcc Doe', $body['bcc'][0]['name']); | ||
$this->assertSame('bcc-simple@mailomat.swiss', $body['bcc'][1]['email']); | ||
|
||
$this->assertSame('replyto@mailomat.swiss', $body['replyTo'][0]['email']); | ||
$this->assertSame('ReplyTo Doe', $body['replyTo'][0]['name']); | ||
$this->assertSame('replyto-simple@mailomat.swiss', $body['replyTo'][1]['email']); | ||
|
||
$this->assertSame('Hello!', $body['subject']); | ||
$this->assertSame('Hello There!', $body['text']); | ||
$this->assertSame('<p>Hello There!</p>', $body['html']); | ||
|
||
return new JsonMockResponse(['messageUuid' => 'foobar'], [ | ||
'http_code' => 202, | ||
]); | ||
}); | ||
|
||
$transport = new MailomatApiTransport(self::KEY, $client); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Hello!') | ||
->from(new Address('from@mailomat.swiss', 'From Doe')) | ||
->to(new Address('to@mailomat.swiss', 'To Doe'), 'to-simple@mailomat.swiss') | ||
->cc(new Address('cc@mailomat.swiss', 'Cc Doe'), 'cc-simple@mailomat.swiss') | ||
->bcc(new Address('bcc@mailomat.swiss', 'Bcc Doe'), 'bcc-simple@mailomat.swiss') | ||
->replyTo(new Address('replyto@mailomat.swiss', 'ReplyTo Doe'), 'replyto-simple@mailomat.swiss') | ||
->text('Hello There!') | ||
->html('<p>Hello There!</p>'); | ||
|
||
$message = $transport->send($mail); | ||
|
||
$this->assertSame('foobar', $message->getMessageId()); | ||
} | ||
|
||
public function testSendThrowsForErrorResponse() | ||
{ | ||
$client = new MockHttpClient(static fn (string $method, string $url, array $options): ResponseInterface => new JsonMockResponse( | ||
[ | ||
'status' => 422, | ||
'violations' => [ | ||
[ | ||
'propertyPath' => '', | ||
'message' => 'You must specify either text or html', | ||
], | ||
[ | ||
'propertyPath' => 'from', | ||
'message' => 'Dieser Wert sollte nicht null sein.', | ||
], | ||
[ | ||
'propertyPath' => 'to[1].email', | ||
'message' => 'Dieser Wert sollte nicht leer sein.', | ||
], | ||
[ | ||
'propertyPath' => 'subject', | ||
'message' => 'Dieser Wert sollte nicht leer sein.', | ||
], | ||
], | ||
], [ | ||
'http_code' => 422, | ||
])); | ||
$transport = new MailomatApiTransport(self::KEY, $client); | ||
$transport->setPort(8984); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Hello!') | ||
->to(new Address('to@mailomat.swiss', 'To Doe')) | ||
->from(new Address('from@mailomat.swiss', 'From Doe')) | ||
->text('Hello There!'); | ||
|
||
$this->expectException(HttpTransportException::class); | ||
$this->expectExceptionMessage('Unable to send an email: You must specify either text or html; (from) Dieser Wert sollte nicht null sein.; (to[1].email) Dieser Wert sollte nicht leer sein.; (subject) Dieser Wert sollte nicht leer sein. (code 422)'); | ||
$transport->send($mail); | ||
} | ||
} |
Oops, something went wrong.