Skip to content

[Mailer] Implement Postal mailer #57903

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 11, 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 @@ -2642,6 +2642,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
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\Postal\Transport\PostalTransportFactory::class => 'mailer.transport_factory.postal',
MailerBridge\Postmark\Transport\PostmarkTransportFactory::class => 'mailer.transport_factory.postmark',
MailerBridge\Resend\Transport\ResendTransportFactory::class => 'mailer.transport_factory.resend',
MailerBridge\Scaleway\Transport\ScalewayTransportFactory::class => 'mailer.transport_factory.scaleway',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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\Postal\Transport\PostalTransportFactory;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
use Symfony\Component\Mailer\Bridge\Resend\Transport\ResendTransportFactory;
use Symfony\Component\Mailer\Bridge\Scaleway\Transport\ScalewayTransportFactory;
Expand Down Expand Up @@ -57,6 +58,7 @@
'mailpace' => MailPaceTransportFactory::class,
'native' => NativeTransportFactory::class,
'null' => NullTransportFactory::class,
'postal' => PostalTransportFactory::class,
'postmark' => PostmarkTransportFactory::class,
'resend' => ResendTransportFactory::class,
'scaleway' => ScalewayTransportFactory::class,
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Postal/.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/Postal/.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/Postal/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/Postal/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.
24 changes: 24 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Postal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Postal Mailer
=============

Provides [Postal Email](https://docs.postalserver.io/) integration for Symfony Mailer.

Configuration example:

```env
# API
MAILER_DSN=postal+api://API_TOKEN@BASE_URL
```

where:
- `API_TOKEN` is your Postal Email API Key
- `BASE_URL` is your Postal installation base URL

Resources
---------

* [Postal API Docs](https://docs.postalserver.io/developer/api)
* [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,107 @@
<?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\Postal\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\Component\Mailer\Bridge\Postal\Transport\PostalApiTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Contracts\HttpClient\ResponseInterface;

class PostalApiTransportTest extends TestCase
{
/**
* @dataProvider getTransportData
*/
public function testToString(PostalApiTransport $transport, string $expected)
{
$this->assertSame($expected, (string) $transport);
}

public static function getTransportData(): array
{
return [
[
(new PostalApiTransport('TOKEN', 'postal.localhost')),
'postal+api://postal.localhost',
],
[
(new PostalApiTransport('TOKEN', 'postal.localhost'))->setPort(99),
'postal+api://postal.localhost:99',
],
];
}

public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://postal.localhost:8984/api/v1/send/message', $url);
$this->assertStringContainsString('X-Server-API-Key: TOKEN', $options['headers'][0] ?? $options['request_headers'][0]);

$body = json_decode($options['body'], true);
$this->assertSame('fabpot@symfony.com', $body['from']);
$this->assertSame('saif.gmati@symfony.com', $body['to'][0]);
$this->assertSame('Hello!', $body['subject']);
$this->assertSame('Hello There!', $body['plain_body']);
$this->assertSame('<h1>Hello There!</h1>', $body['html_body']);
$this->assertCount(1, $body['attachments']);
$this->assertSame('attachment.txt', $body['attachments'][0]['name']);
$this->assertSame('text/plain', $body['attachments'][0]['content_type']);
$this->assertSame(base64_encode('some attachment'), $body['attachments'][0]['data']);
$this->assertSame('foo@bar.fr', $body['reply_to']);

return new JsonMockResponse(['message_id' => 'foobar'], [
'http_code' => 200,
]);
});
$transport = new PostalApiTransport('TOKEN', 'postal.localhost', $client);
$transport->setPort(8984);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->replyTo(new Address('foo@bar.fr', 'Foo'))
->text('Hello There!')
->html('<h1>Hello There!</h1>')
->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'));

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
}

public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
return new JsonMockResponse(['message' => 'i\'m a teapot'], [
'http_code' => 418,
]);
});
$transport = new PostalApiTransport('TOKEN', 'postal.localhost', $client);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');

$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\Postal\Tests\Transport;

use Psr\Log\NullLogger;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Mailer\Bridge\Postal\Transport\PostalApiTransport;
use Symfony\Component\Mailer\Bridge\Postal\Transport\PostalTransportFactory;
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;

class PostalTransportFactoryTest extends TransportFactoryTestCase
{
public function getFactory(): TransportFactoryInterface
{
return new PostalTransportFactory(null, new MockHttpClient(), new NullLogger());
}

public static function supportsProvider(): iterable
{
yield [
new Dsn('postal+api', 'postal.localhost'),
true,
];

yield [
new Dsn('postal', 'postal.localhost'),
true,
];
}

public static function createProvider(): iterable
{
$logger = new NullLogger();

yield [
new Dsn('postal+api', 'postal.localhost', null, self::PASSWORD),
(new PostalApiTransport(self::PASSWORD, 'postal.localhost', new MockHttpClient(), null, $logger)),
];

yield [
new Dsn('postal', 'postal.localhost', null, self::PASSWORD),
(new PostalApiTransport(self::PASSWORD, 'postal.localhost', new MockHttpClient(), null, $logger)),
];
}

public static function unsupportedSchemeProvider(): iterable
{
yield [
new Dsn('postal+foo', 'postal.localhost', null, self::PASSWORD),
'The "postal+foo" scheme is not supported; supported schemes for mailer "postal" are: "postal", "postal+api".',
];
}

public static function incompleteDsnProvider(): iterable
{
yield [new Dsn('postal+api', 'postal.localhost', null)];
}
}
Loading