[Mailer] Added Sendinblue bridge
This commit is contained in:
parent
7cb8ba585d
commit
836a20350b
@ -83,6 +83,7 @@ use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory
|
|||||||
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
|
||||||
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
|
||||||
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
|
||||||
|
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
|
||||||
use Symfony\Component\Mailer\Mailer;
|
use Symfony\Component\Mailer\Mailer;
|
||||||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
|
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
|
||||||
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
|
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
|
||||||
@ -2128,6 +2129,7 @@ class FrameworkExtension extends Extension
|
|||||||
MailgunTransportFactory::class => 'mailer.transport_factory.mailgun',
|
MailgunTransportFactory::class => 'mailer.transport_factory.mailgun',
|
||||||
PostmarkTransportFactory::class => 'mailer.transport_factory.postmark',
|
PostmarkTransportFactory::class => 'mailer.transport_factory.postmark',
|
||||||
SendgridTransportFactory::class => 'mailer.transport_factory.sendgrid',
|
SendgridTransportFactory::class => 'mailer.transport_factory.sendgrid',
|
||||||
|
SendinblueTransportFactory::class => 'mailer.transport_factory.sendinblue',
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($classToServices as $class => $service) {
|
foreach ($classToServices as $class => $service) {
|
||||||
|
@ -17,6 +17,7 @@ use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory
|
|||||||
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
|
||||||
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
|
||||||
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
|
||||||
|
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
|
||||||
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
|
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
|
||||||
use Symfony\Component\Mailer\Transport\NativeTransportFactory;
|
use Symfony\Component\Mailer\Transport\NativeTransportFactory;
|
||||||
use Symfony\Component\Mailer\Transport\NullTransportFactory;
|
use Symfony\Component\Mailer\Transport\NullTransportFactory;
|
||||||
@ -65,6 +66,10 @@ return static function (ContainerConfigurator $container) {
|
|||||||
->parent('mailer.transport_factory.abstract')
|
->parent('mailer.transport_factory.abstract')
|
||||||
->tag('mailer.transport_factory')
|
->tag('mailer.transport_factory')
|
||||||
|
|
||||||
|
->set('mailer.transport_factory.sendinblue', SendinblueTransportFactory::class)
|
||||||
|
->parent('mailer.transport_factory.abstract')
|
||||||
|
->tag('mailer.transport_factory')
|
||||||
|
|
||||||
->set('mailer.transport_factory.smtp', EsmtpTransportFactory::class)
|
->set('mailer.transport_factory.smtp', EsmtpTransportFactory::class)
|
||||||
->parent('mailer.transport_factory.abstract')
|
->parent('mailer.transport_factory.abstract')
|
||||||
->tag('mailer.transport_factory', ['priority' => -100])
|
->tag('mailer.transport_factory', ['priority' => -100])
|
||||||
|
4
src/Symfony/Component/Mailer/Bridge/Sendinblue/.gitattributes
vendored
Normal file
4
src/Symfony/Component/Mailer/Bridge/Sendinblue/.gitattributes
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/Tests export-ignore
|
||||||
|
/phpunit.xml.dist export-ignore
|
||||||
|
/.gitattributes export-ignore
|
||||||
|
/.gitignore export-ignore
|
3
src/Symfony/Component/Mailer/Bridge/Sendinblue/.gitignore
vendored
Normal file
3
src/Symfony/Component/Mailer/Bridge/Sendinblue/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
vendor/
|
||||||
|
composer.lock
|
||||||
|
phpunit.xml
|
@ -0,0 +1,7 @@
|
|||||||
|
CHANGELOG
|
||||||
|
=========
|
||||||
|
|
||||||
|
5.2.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added the bridge
|
19
src/Symfony/Component/Mailer/Bridge/Sendinblue/LICENSE
Normal file
19
src/Symfony/Component/Mailer/Bridge/Sendinblue/LICENSE
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2019-2020 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.
|
53
src/Symfony/Component/Mailer/Bridge/Sendinblue/README.md
Normal file
53
src/Symfony/Component/Mailer/Bridge/Sendinblue/README.md
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
Sendinblue Bridge
|
||||||
|
=================
|
||||||
|
|
||||||
|
Provides Sendinblue integration for Symfony Mailer.
|
||||||
|
|
||||||
|
|
||||||
|
Configuration example:
|
||||||
|
|
||||||
|
```env
|
||||||
|
# API
|
||||||
|
MAILER_DSN=sendinblue+api://$SENDINBLUE_API_KEY@default
|
||||||
|
|
||||||
|
# SMTP
|
||||||
|
MAILER_DSN=sendinblue+smtp://$SENDINBLUE_USERNAME:$SENDINBLUE_PASSWORD@default
|
||||||
|
```
|
||||||
|
|
||||||
|
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 [Sendinblue API documentation](https://developers.sendinblue.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)
|
@ -0,0 +1,142 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\Mailer\Bridge\Sendinblue\Tests\Transport;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\HttpClient\MockHttpClient;
|
||||||
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
||||||
|
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueApiTransport;
|
||||||
|
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 SendinblueApiTransportTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider getTransportData
|
||||||
|
*/
|
||||||
|
public function testToString(SendinblueApiTransport $transport, string $expected)
|
||||||
|
{
|
||||||
|
$this->assertSame($expected, (string) $transport);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTransportData()
|
||||||
|
{
|
||||||
|
yield [
|
||||||
|
new SendinblueApiTransport('ACCESS_KEY'),
|
||||||
|
'sendinblue+api://api.sendinblue.com',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
(new SendinblueApiTransport('ACCESS_KEY'))->setHost('example.com'),
|
||||||
|
'sendinblue+api://example.com',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
(new SendinblueApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99),
|
||||||
|
'sendinblue+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 SendinblueApiTransport('ACCESS_KEY');
|
||||||
|
$method = new \ReflectionMethod(SendinblueApiTransport::class, 'getPayload');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
$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.sendinblue.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 SendinblueApiTransport('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.sendinblue.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 SendinblueApiTransport('ACCESS_KEY', $client);
|
||||||
|
$transport->setPort(8984);
|
||||||
|
|
||||||
|
$dataPart = new DataPart('body');
|
||||||
|
$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')
|
||||||
|
->attachPart($dataPart)
|
||||||
|
;
|
||||||
|
|
||||||
|
$message = $transport->send($mail);
|
||||||
|
|
||||||
|
$this->assertSame('foobar', $message->getMessageId());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\Mailer\Bridge\Sendinblue\Tests\Transport;
|
||||||
|
|
||||||
|
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueApiTransport;
|
||||||
|
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueSmtpTransport;
|
||||||
|
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
|
||||||
|
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
|
||||||
|
use Symfony\Component\Mailer\Transport\Dsn;
|
||||||
|
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
|
||||||
|
|
||||||
|
class SendinblueTransportFactoryTest extends TransportFactoryTestCase
|
||||||
|
{
|
||||||
|
public function getFactory(): TransportFactoryInterface
|
||||||
|
{
|
||||||
|
return new SendinblueTransportFactory($this->getDispatcher(), $this->getClient(), $this->getLogger());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsProvider(): iterable
|
||||||
|
{
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue', 'default'),
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue+smtp', 'default'),
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue+smtp', 'example.com'),
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue+api', 'default'),
|
||||||
|
true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createProvider(): iterable
|
||||||
|
{
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue', 'default', self::USER, self::PASSWORD),
|
||||||
|
new SendinblueSmtpTransport(self::USER, self::PASSWORD, $this->getDispatcher(), $this->getLogger()),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue+smtp', 'default', self::USER, self::PASSWORD),
|
||||||
|
new SendinblueSmtpTransport(self::USER, self::PASSWORD, $this->getDispatcher(), $this->getLogger()),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue+smtp', 'default', self::USER, self::PASSWORD, 465),
|
||||||
|
new SendinblueSmtpTransport(self::USER, self::PASSWORD, $this->getDispatcher(), $this->getLogger()),
|
||||||
|
];
|
||||||
|
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue+api', 'default', self::USER),
|
||||||
|
new SendinblueApiTransport(self::USER, $this->getClient(), $this->getDispatcher(), $this->getLogger()),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unsupportedSchemeProvider(): iterable
|
||||||
|
{
|
||||||
|
yield [
|
||||||
|
new Dsn('sendinblue+foo', 'default', self::USER, self::PASSWORD),
|
||||||
|
'The "sendinblue+foo" scheme is not supported; supported schemes for mailer "sendinblue" are: "sendinblue", "sendinblue+smtp", "sendinblue+api".',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function incompleteDsnProvider(): iterable
|
||||||
|
{
|
||||||
|
yield [new Dsn('sendinblue+smtp', 'default', self::USER)];
|
||||||
|
|
||||||
|
yield [new Dsn('sendinblue+smtp', 'default', null, self::PASSWORD)];
|
||||||
|
|
||||||
|
yield [new Dsn('sendinblue+api', 'default')];
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,175 @@
|
|||||||
|
<?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\Sendinblue\Transport;
|
||||||
|
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
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\Mailer\SentMessage;
|
||||||
|
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
|
||||||
|
use Symfony\Component\Mime\Address;
|
||||||
|
use Symfony\Component\Mime\Email;
|
||||||
|
use Symfony\Component\Mime\Header\Headers;
|
||||||
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||||
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Yann LUCAS
|
||||||
|
*/
|
||||||
|
final class SendinblueApiTransport extends AbstractApiTransport
|
||||||
|
{
|
||||||
|
private $key;
|
||||||
|
|
||||||
|
public function __construct(string $key, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null)
|
||||||
|
{
|
||||||
|
$this->key = $key;
|
||||||
|
|
||||||
|
parent::__construct($client, $dispatcher, $logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('sendinblue+api://%s', $this->getEndpoint());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
|
||||||
|
{
|
||||||
|
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v3/smtp/email', [
|
||||||
|
'json' => $this->getPayload($email, $envelope),
|
||||||
|
'headers' => [
|
||||||
|
'api-key' => $this->key,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = $response->toArray(false);
|
||||||
|
if (201 !== $response->getStatusCode()) {
|
||||||
|
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $response->getStatusCode()), $response);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sentMessage->setMessageId($result['messageId']);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function stringifyAddresses(array $addresses): array
|
||||||
|
{
|
||||||
|
$stringifiedAddresses = [];
|
||||||
|
foreach ($addresses as $address) {
|
||||||
|
$stringifiedAddresses[] = $this->stringifyAddress($address);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $stringifiedAddresses;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getPayload(Email $email, Envelope $envelope): array
|
||||||
|
{
|
||||||
|
$payload = [
|
||||||
|
'sender' => $this->stringifyAddress($envelope->getSender()),
|
||||||
|
'to' => $this->stringifyAddresses($this->getRecipients($email, $envelope)),
|
||||||
|
'subject' => $email->getSubject(),
|
||||||
|
];
|
||||||
|
if ($attachements = $this->prepareAttachments($email)) {
|
||||||
|
$payload['attachment'] = $attachements;
|
||||||
|
}
|
||||||
|
if ($emails = $email->getReplyTo()) {
|
||||||
|
$payload['replyTo'] = current($this->stringifyAddresses($emails));
|
||||||
|
}
|
||||||
|
if ($emails = $email->getCc()) {
|
||||||
|
$payload['cc'] = $this->stringifyAddresses($emails);
|
||||||
|
}
|
||||||
|
if ($emails = $email->getBcc()) {
|
||||||
|
$payload['bcc'] = $this->stringifyAddresses($emails);
|
||||||
|
}
|
||||||
|
if ($email->getTextBody()) {
|
||||||
|
$payload['textContent'] = $email->getTextBody();
|
||||||
|
}
|
||||||
|
if ($email->getHtmlBody()) {
|
||||||
|
$payload['htmlContent'] = $email->getHtmlBody();
|
||||||
|
}
|
||||||
|
if ($headersAndTags = $this->prepareHeadersAndTags($email->getHeaders())) {
|
||||||
|
$payload = array_merge($payload, $headersAndTags);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function prepareAttachments(Email $email): array
|
||||||
|
{
|
||||||
|
$attachments = [];
|
||||||
|
foreach ($email->getAttachments() as $attachment) {
|
||||||
|
$headers = $attachment->getPreparedHeaders();
|
||||||
|
$filename = $headers->getHeaderParameter('Content-Disposition', 'filename');
|
||||||
|
|
||||||
|
$att = [
|
||||||
|
'content' => str_replace("\r\n", '', $attachment->bodyToString()),
|
||||||
|
'name' => $filename,
|
||||||
|
];
|
||||||
|
|
||||||
|
$attachments[] = $att;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attachments;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function prepareHeadersAndTags(Headers $headers): array
|
||||||
|
{
|
||||||
|
$headersAndTags = [];
|
||||||
|
$headersToBypass = ['from', 'to', 'cc', 'bcc', 'subject', 'reply-to', 'content-type', 'accept', 'api-key'];
|
||||||
|
foreach ($headers->all() as $name => $header) {
|
||||||
|
if (\in_array($name, $headersToBypass, true)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($header instanceof TagHeader) {
|
||||||
|
$headersAndTags['tags'][] = $header->getValue();
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($header instanceof MetadataHeader) {
|
||||||
|
$headersAndTags['headers']['X-Mailin-'.ucfirst(strtolower($header->getKey()))] = $header->getValue();
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ('templateid' === $name) {
|
||||||
|
$headersAndTags[$header->getName()] = (int) $header->getValue();
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ('params' === $name) {
|
||||||
|
$headersAndTags[$header->getName()] = $header->getParameters();
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$headersAndTags['headers'][$name] = $header->getBodyAsString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $headersAndTags;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function stringifyAddress(Address $address): array
|
||||||
|
{
|
||||||
|
$stringifiedAddress = ['email' => $address->getAddress()];
|
||||||
|
|
||||||
|
if ($address->getName()) {
|
||||||
|
$stringifiedAddress['name'] = $address->getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $stringifiedAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getEndpoint(): ?string
|
||||||
|
{
|
||||||
|
return ($this->host ?: 'api.sendinblue.com').($this->port ? ':'.$this->port : '');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
<?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\Sendinblue\Transport;
|
||||||
|
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
||||||
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Yann LUCAS
|
||||||
|
*/
|
||||||
|
final class SendinblueSmtpTransport extends EsmtpTransport
|
||||||
|
{
|
||||||
|
public function __construct(string $username, string $password, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null)
|
||||||
|
{
|
||||||
|
parent::__construct('smtp-relay.sendinblue.com', 465, true, $dispatcher, $logger);
|
||||||
|
|
||||||
|
$this->setUsername($username);
|
||||||
|
$this->setPassword($password);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
<?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\Sendinblue\Transport;
|
||||||
|
|
||||||
|
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
|
||||||
|
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
|
||||||
|
use Symfony\Component\Mailer\Transport\Dsn;
|
||||||
|
use Symfony\Component\Mailer\Transport\TransportInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Yann LUCAS
|
||||||
|
*/
|
||||||
|
final class SendinblueTransportFactory extends AbstractTransportFactory
|
||||||
|
{
|
||||||
|
public function create(Dsn $dsn): TransportInterface
|
||||||
|
{
|
||||||
|
if (!\in_array($dsn->getScheme(), $this->getSupportedSchemes(), true)) {
|
||||||
|
throw new UnsupportedSchemeException($dsn, 'sendinblue', $this->getSupportedSchemes());
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($dsn->getScheme()) {
|
||||||
|
default:
|
||||||
|
case 'sendinblue':
|
||||||
|
case 'sendinblue+smtp':
|
||||||
|
$transport = SendinblueSmtpTransport::class;
|
||||||
|
break;
|
||||||
|
case 'sendinblue+api':
|
||||||
|
return (new SendinblueApiTransport($this->getUser($dsn), $this->client, $this->dispatcher, $this->logger))
|
||||||
|
->setHost('default' === $dsn->getHost() ? null : $dsn->getHost())
|
||||||
|
->setPort($dsn->getPort())
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new $transport($this->getUser($dsn), $this->getPassword($dsn), $this->dispatcher, $this->logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getSupportedSchemes(): array
|
||||||
|
{
|
||||||
|
return ['sendinblue', 'sendinblue+smtp', 'sendinblue+api'];
|
||||||
|
}
|
||||||
|
}
|
37
src/Symfony/Component/Mailer/Bridge/Sendinblue/composer.json
Normal file
37
src/Symfony/Component/Mailer/Bridge/Sendinblue/composer.json
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"name": "symfony/sendinblue-mailer",
|
||||||
|
"type": "symfony-bridge",
|
||||||
|
"description": "Symfony Sendinblue Mailer Bridge",
|
||||||
|
"keywords": [],
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Fabien Potencier",
|
||||||
|
"email": "fabien@symfony.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=7.2.5",
|
||||||
|
"symfony/mailer": "^5.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"symfony/http-client": "^4.4|^5.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Sendinblue\\": "" },
|
||||||
|
"exclude-from-classmap": [
|
||||||
|
"/Tests/"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "5.2-dev"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
|
||||||
|
backupGlobals="false"
|
||||||
|
colors="true"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
failOnRisky="true"
|
||||||
|
failOnWarning="true"
|
||||||
|
>
|
||||||
|
<php>
|
||||||
|
<ini name="error_reporting" value="-1" />
|
||||||
|
</php>
|
||||||
|
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Symfony Sendinblue Mailer Test Suite">
|
||||||
|
<directory>./Tests/</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<filter>
|
||||||
|
<whitelist>
|
||||||
|
<directory>./</directory>
|
||||||
|
<exclude>
|
||||||
|
<directory>./Resources</directory>
|
||||||
|
<directory>./Tests</directory>
|
||||||
|
<directory>./vendor</directory>
|
||||||
|
</exclude>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
</phpunit>
|
@ -48,6 +48,10 @@ class UnsupportedSchemeException extends LogicException
|
|||||||
'class' => Bridge\Mailjet\Transport\MailjetTransportFactory::class,
|
'class' => Bridge\Mailjet\Transport\MailjetTransportFactory::class,
|
||||||
'package' => 'symfony/mailjet-mailer',
|
'package' => 'symfony/mailjet-mailer',
|
||||||
],
|
],
|
||||||
|
'sendinblue' => [
|
||||||
|
'class' => Bridge\Sendinblue\Transport\SendinblueTransportFactory::class,
|
||||||
|
'package' => 'symfony/sendinblue-mailer',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(Dsn $dsn, string $name = null, array $supported = [])
|
public function __construct(Dsn $dsn, string $name = null, array $supported = [])
|
||||||
|
@ -19,6 +19,7 @@ use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
|
|||||||
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
|
||||||
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
|
||||||
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
|
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
|
||||||
|
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
|
||||||
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
|
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
|
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
|
||||||
use Symfony\Component\Mailer\Transport\Dsn;
|
use Symfony\Component\Mailer\Transport\Dsn;
|
||||||
@ -48,6 +49,7 @@ class Transport
|
|||||||
PostmarkTransportFactory::class,
|
PostmarkTransportFactory::class,
|
||||||
SendgridTransportFactory::class,
|
SendgridTransportFactory::class,
|
||||||
MailjetTransportFactory::class,
|
MailjetTransportFactory::class,
|
||||||
|
SendinblueTransportFactory::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
private $factories;
|
private $factories;
|
||||||
|
Reference in New Issue
Block a user