-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mailer] New Brevo mailer bridge (formerly Sendinblue) #50302
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
========= | ||
|
||
6.4 | ||
--- | ||
|
||
* Added the bridge as a replacement of the deprecated Sendinblue one. | ||
This file contains hidden or 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) 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. |
This file contains hidden or 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,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(); | ||
->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) |
146 changes: 146 additions & 0 deletions
146
src/Symfony/Component/Mailer/Bridge/Brevo/Tests/Transport/BrevoApiTransportTest.php
This file contains hidden or 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,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()); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/Symfony/Component/Mailer/Bridge/Brevo/Tests/Transport/BrevoTransportFactoryTest.php
This file contains hidden or 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,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')]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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