Skip to content
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

[Mailer] Add mailomat bridge #57456

Merged
merged 1 commit into from
Jun 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
MailerBridge\MailerSend\Transport\MailerSendTransportFactory::class => 'mailer.transport_factory.mailersend',
MailerBridge\Mailgun\Transport\MailgunTransportFactory::class => 'mailer.transport_factory.mailgun',
MailerBridge\Mailjet\Transport\MailjetTransportFactory::class => 'mailer.transport_factory.mailjet',
MailerBridge\Mailomat\Transport\MailomatTransportFactory::class => 'mailer.transport_factory.mailomat',
MailerBridge\MailPace\Transport\MailPaceTransportFactory::class => 'mailer.transport_factory.mailpace',
MailerBridge\Mailchimp\Transport\MandrillTransportFactory::class => 'mailer.transport_factory.mailchimp',
MailerBridge\Postmark\Transport\PostmarkTransportFactory::class => 'mailer.transport_factory.postmark',
Expand All @@ -2643,6 +2644,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
MailerBridge\MailerSend\Webhook\MailerSendRequestParser::class => 'mailer.webhook.request_parser.mailersend',
MailerBridge\Mailgun\Webhook\MailgunRequestParser::class => 'mailer.webhook.request_parser.mailgun',
MailerBridge\Mailjet\Webhook\MailjetRequestParser::class => 'mailer.webhook.request_parser.mailjet',
MailerBridge\Mailomat\Webhook\MailomatRequestParser::class => 'mailer.webhook.request_parser.mailomat',
MailerBridge\Postmark\Webhook\PostmarkRequestParser::class => 'mailer.webhook.request_parser.postmark',
MailerBridge\Resend\Webhook\ResendRequestParser::class => 'mailer.webhook.request_parser.resend',
MailerBridge\Sendgrid\Webhook\SendgridRequestParser::class => 'mailer.webhook.request_parser.sendgrid',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailomat\Transport\MailomatTransportFactory;
use Symfony\Component\Mailer\Bridge\MailPace\Transport\MailPaceTransportFactory;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
use Symfony\Component\Mailer\Bridge\Resend\Transport\ResendTransportFactory;
Expand Down Expand Up @@ -52,6 +53,7 @@
'mailersend' => MailerSendTransportFactory::class,
'mailgun' => MailgunTransportFactory::class,
'mailjet' => MailjetTransportFactory::class,
'mailomat' => MailomatTransportFactory::class,
'mailpace' => MailPaceTransportFactory::class,
'native' => NativeTransportFactory::class,
'null' => NullTransportFactory::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Symfony\Component\Mailer\Bridge\Mailgun\Webhook\MailgunRequestParser;
use Symfony\Component\Mailer\Bridge\Mailjet\RemoteEvent\MailjetPayloadConverter;
use Symfony\Component\Mailer\Bridge\Mailjet\Webhook\MailjetRequestParser;
use Symfony\Component\Mailer\Bridge\Mailomat\RemoteEvent\MailomatPayloadConverter;
use Symfony\Component\Mailer\Bridge\Mailomat\Webhook\MailomatRequestParser;
use Symfony\Component\Mailer\Bridge\Postmark\RemoteEvent\PostmarkPayloadConverter;
use Symfony\Component\Mailer\Bridge\Postmark\Webhook\PostmarkRequestParser;
use Symfony\Component\Mailer\Bridge\Resend\RemoteEvent\ResendPayloadConverter;
Expand Down Expand Up @@ -48,6 +50,11 @@
->args([service('mailer.payload_converter.mailjet')])
->alias(MailjetRequestParser::class, 'mailer.webhook.request_parser.mailjet')

->set('mailer.payload_converter.mailomat', MailomatPayloadConverter::class)
->set('mailer.webhook.request_parser.mailomat', MailomatRequestParser::class)
->args([service('mailer.payload_converter.mailomat')])
->alias(MailomatRequestParser::class, 'mailer.webhook.request_parser.mailomat')

->set('mailer.payload_converter.postmark', PostmarkPayloadConverter::class)
->set('mailer.webhook.request_parser.postmark', PostmarkRequestParser::class)
->args([service('mailer.payload_converter.postmark')])
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailomat/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.git* export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailomat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailomat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.2
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailomat/LICENSE
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.
71 changes: 71 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailomat/README.md
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)
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;
}
}
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);
}
}
Loading
Loading