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] New Brevo mailer bridge (formerly Sendinblue) #50302

Merged
merged 1 commit into from
Jul 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
}

$classToServices = [
MailerBridge\Brevo\Transport\BrevoTransportFactory::class => 'mailer.transport_factory.brevo',
MailerBridge\Google\Transport\GmailTransportFactory::class => 'mailer.transport_factory.gmail',
MailerBridge\Infobip\Transport\InfobipTransportFactory::class => 'mailer.transport_factory.infobip',
MailerBridge\MailerSend\Transport\MailerSendTransportFactory::class => 'mailer.transport_factory.mailersend',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesTransportFactory;
use Symfony\Component\Mailer\Bridge\Brevo\Transport\BrevoTransportFactory;
use Symfony\Component\Mailer\Bridge\Google\Transport\GmailTransportFactory;
use Symfony\Component\Mailer\Bridge\Infobip\Transport\InfobipTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
Expand Down Expand Up @@ -44,6 +45,10 @@
->parent('mailer.transport_factory.abstract')
->tag('mailer.transport_factory')

->set('mailer.transport_factory.brevo', BrevoTransportFactory::class)
->parent('mailer.transport_factory.abstract')
->tag('mailer.transport_factory')

->set('mailer.transport_factory.gmail', GmailTransportFactory::class)
->parent('mailer.transport_factory.abstract')
->tag('mailer.transport_factory')
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Brevo/.gitattributes
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Brevo/.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/Brevo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.4
---

* Added the bridge as a replacement of the deprecated Sendinblue one.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Added the bridge as a replacement of the deprecated Sendinblue one.
* Add the bridge

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PEtanguy you forgot this change when fixing requested things

19 changes: 19 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Brevo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023-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.
56 changes: 56 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Brevo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Brevo Bridge
============

Provides Brevo integration for Symfony Mailer.
This was added uppon Sendinblue's rebranding to Brevo.

Configuration example:

```env
# SMTP
MAILER_DSN=brevo+smtp://USERNAME:PASSWORD@default

# API
MAILER_DSN=brevo+api://KEY@default
```

where:
- `KEY` is your Brevo API Key

With API, you can use custom headers.

```php
$params = ['param1' => 'foo', 'param2' => 'bar'];
$json = json_encode(['"custom_header_1' => 'custom_value_1']);

$email = new Email();
$email
->getHeaders()
->add(new MetadataHeader('custom', $json))
->add(new TagHeader('TagInHeaders1'))
->add(new TagHeader('TagInHeaders2'))
->addTextHeader('sender.ip', '1.2.3.4')
->addTextHeader('templateId', 1)
->addParameterizedHeader('params', 'params', $params)
->addTextHeader('foo', 'bar')
;
```

This example allow you to set :

* templateId
* params
* tags
* headers
* sender.ip
* X-Mailin-Custom

For more informations, you can refer to [Brevo API documentation](https://developers.brevo.com/reference/sendtransacemail).

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,146 @@
<?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\Brevo\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Brevo\Transport\BrevoApiTransport;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

public static function getTransportData()
{
yield [
new BrevoApiTransport('ACCESS_KEY'),
'brevo+api://api.brevo.com',
];

yield [
(new BrevoApiTransport('ACCESS_KEY'))->setHost('example.com'),
'brevo+api://example.com',
];

yield [
(new BrevoApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99),
'brevo+api://example.com:99',
];
}

public function testCustomHeader()
{
$params = ['param1' => 'foo', 'param2' => 'bar'];
$json = json_encode(['"custom_header_1' => 'custom_value_1']);

$email = new Email();
$email->getHeaders()
->add(new MetadataHeader('custom', $json))
->add(new TagHeader('TagInHeaders'))
->addTextHeader('templateId', 1)
->addParameterizedHeader('params', 'params', $params)
->addTextHeader('foo', 'bar');
$envelope = new Envelope(new Address('alice@system.com', 'Alice'), [new Address('bob@system.com', 'Bob')]);

$transport = new BrevoApiTransport('ACCESS_KEY');
$method = new \ReflectionMethod(BrevoApiTransport::class, 'getPayload');
$payload = $method->invoke($transport, $email, $envelope);

$this->assertArrayHasKey('X-Mailin-Custom', $payload['headers']);
$this->assertEquals($json, $payload['headers']['X-Mailin-Custom']);

$this->assertArrayHasKey('tags', $payload);
$this->assertEquals('TagInHeaders', current($payload['tags']));
$this->assertArrayHasKey('templateId', $payload);
$this->assertEquals(1, $payload['templateId']);
$this->assertArrayHasKey('params', $payload);
$this->assertEquals('foo', $payload['params']['param1']);
$this->assertEquals('bar', $payload['params']['param2']);
$this->assertArrayHasKey('foo', $payload['headers']);
$this->assertEquals('bar', $payload['headers']['foo']);
}

public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.brevo.com:8984/v3/smtp/email', $url);
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]);

return new MockResponse(json_encode(['message' => 'i\'m a teapot']), [
'http_code' => 418,
'response_headers' => [
'content-type' => 'application/json',
],
]);
});

$transport = new BrevoApiTransport('ACCESS_KEY', $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'))
->text('Hello There!');

$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}

public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.brevo.com:8984/v3/smtp/email', $url);
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]);

return new MockResponse(json_encode(['messageId' => 'foobar']), [
'http_code' => 201,
]);
});

$transport = new BrevoApiTransport('ACCESS_KEY', $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'))
->text('Hello here!')
->html('Hello there!')
->addCc('foo@bar.fr')
->addBcc('foo@bar.fr')
->addReplyTo('foo@bar.fr')
->addPart(new DataPart('body'));

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

$this->assertSame('foobar', $message->getMessageId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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\Brevo\Tests\Transport;

use Psr\Log\NullLogger;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Mailer\Bridge\Brevo\Transport\BrevoApiTransport;
use Symfony\Component\Mailer\Bridge\Brevo\Transport\BrevoSmtpTransport;
use Symfony\Component\Mailer\Bridge\Brevo\Transport\BrevoTransportFactory;
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;

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

public static function supportsProvider(): iterable
{
yield [
new Dsn('brevo', 'default'),
true,
];

yield [
new Dsn('brevo+smtp', 'default'),
true,
];

yield [
new Dsn('brevo+smtp', 'example.com'),
true,
];

yield [
new Dsn('brevo+api', 'default'),
true,
];
}

public static function createProvider(): iterable
{
yield [
new Dsn('brevo', 'default', self::USER, self::PASSWORD),
new BrevoSmtpTransport(self::USER, self::PASSWORD, null, new NullLogger()),
];

yield [
new Dsn('brevo+smtp', 'default', self::USER, self::PASSWORD),
new BrevoSmtpTransport(self::USER, self::PASSWORD, null, new NullLogger()),
];

yield [
new Dsn('brevo+smtp', 'default', self::USER, self::PASSWORD, 465),
new BrevoSmtpTransport(self::USER, self::PASSWORD, null, new NullLogger()),
];

yield [
new Dsn('brevo+api', 'default', self::USER),
new BrevoApiTransport(self::USER, new MockHttpClient(), null, new NullLogger()),
];
}

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

public static function incompleteDsnProvider(): iterable
{
yield [new Dsn('brevo+smtp', 'default', self::USER)];

yield [new Dsn('brevo+smtp', 'default', null, self::PASSWORD)];

yield [new Dsn('brevo+api', 'default')];
}
}