8000 [Mailer] Added Sendinblue bridge · symfony/symfony@1d3c246 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d3c246

Browse files
committed
[Mailer] Added Sendinblue bridge
1 parent 7cb8ba5 commit 1d3c246

13 files changed

+666
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Sendinblue Bridge
2+
=================
3+
4+
Provides Sendinblue integration for Symfony Mailer.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10< B41A span class="diff-text-marker">+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Symfony\Component\Mailer\Bridge\Sendinblue\Tests\Transport;
4+
5+
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueApiTransport;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\HttpClient\MockHttpClient;
8+
use Symfony\Component\HttpClient\Response\MockResponse;
9+
use Symfony\Component\Mailer\Envelope;
10+
use Symfony\Component\Mailer\Exception\HttpTransportException;
11+
use Symfony\Component\Mailer\Header\MetadataHeader;
12+
use Symfony\Component\Mailer\Header\TagHeader;
13+
use Symfony\Component\Mime\Address;
14+
use Symfony\Component\Mime\Email;
15+
use Symfony\Component\Mime\Part\DataPart;
16+
use Symfony\Contracts\HttpClient\ResponseInterface;
17+
18+
class SendinblueApiTransportTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider getTransportData
22+
*/
23+
public function testToString(SendinblueApiTransport $transport, string $expected)
24+
{
25+
$this->assertSame($expected, (string) $transport);
26+
}
27+
28+
public function getTransportData()
29+
{
30+
yield [
31+
new SendinblueApiTransport('ACCESS_KEY'),
32+
'sendinblue+api://api.sendinblue.com',
33+
];
34+
35+
yield [
36+
(new SendinblueApiTransport('ACCESS_KEY'))->setHost('example.com'),
37+
'sendinblue+api://example.com',
38+
];
39+
40+
yield [
41+
(new SendinblueApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99),
42+
'sendinblue+api://example.com:99',
43+
];
44+
}
45+
46+
public function testCustomHeader()
47+
{
48+
$params = ['param1' => 'foo', 'param2' => 'bar'];
49+
$json = json_encode(['"custom_header_1' => 'custom_value_1']);
50+
51+
$email = new Email();
52+
$email->getHeaders()
53+
->add(new MetadataHeader('custom', $json))
54+
->add(new TagHeader('TagInHeaders'))
55+
->addTextHeader('templateId', 1)
56+
->addParameterizedHeader('params', 'params', $params)
57+
->addTextHeader('foo', 'bar')
58+
;
59+
$envelope = new Envelope(new Address('alice@system.com', 'Alice'), [new Address('bob@system.com', 'Bob')]);
60+
61+
$transport = new SendinblueApiTransport('ACCESS_KEY');
62+
$method = new \ReflectionMethod(SendinblueApiTransport::class, 'getPayload');
63+
$method->setAccessible(true);
64+
$payload = $method->invoke($transport, $email, $envelope);
65+
66+
$this->assertArrayHasKey('X-Mailin-Custom', $payload['headers']);
67+
$this->assertEquals($json, $payload['headers']['X-Mailin-Custom']);
68+
69+
$this->assertArrayHasKey('tags', $payload);
70+
$this->assertEquals('TagInHeaders', current($payload['tags']));
71+
$this->assertArrayHasKey('templateId', $payload);
72+
$this->assertEquals(1, $payload['templateId']);
73+
$this->assertArrayHasKey('params', $payload);
74+
$this->assertEquals('foo', $payload['params']['param1']);
75+
$this->assertEquals('bar', $payload['params']['param2']);
76+
$this->assertArrayHasKey('foo', $payload['headers']);
77+
$this->assertEquals('bar', $payload['headers']['foo']);
78+
}
79+
80+
public function testSendThrowsForErrorResponse()
81+
{
82+
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
83+
$this->assertSame('POST', $method);
84+
$this->assertSame('https://api.sendinblue.com:8984/v3/smtp/email', $url);
85+
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]);
86+
87+
return new MockResponse(json_encode(['message' => 'i\'m a teapot']), [
88+
'http_code' => 418,
89+
'response_headers' => [
90+
'content-type' => 'application/json',
91+
],
92+
]);
93+
});
94+
95+
$transport = new SendinblueApiTransport('ACCESS_KEY', $client);
96+
$transport->setPort(8984);
97+
98+
$mail = new Email();
99+
$mail->subject('Hello!')
100+
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
101+
->from(new Address('fabpot@symfony.com', 'Fabien'))
102+
->text('Hello There!')
103+
;
104+
105+
$this->expectException(HttpTransportException::class);
106+
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
107+
$transport->send($mail);
108+
}
109+
110+
public function testSend()
111+
{
112+
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
113+
$this->assertSame('POST', $method);
114+
$this->assertSame('https://api.sendinblue.com:8984/v3/smtp/email', $url);
115+
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]);
116+
117+
return new MockResponse(json_encode(['messageId' => 'foobar']), [
118+
'http_code' => 201,
119+
]);
120+
});
121+
122+
$transport = new SendinblueApiTransport('ACCESS_KEY', $client);
123+
$transport->setPort(8984);
124+
125+
$dataPart = new DataPart('body');
126+
$mail = new Email();
127+
$mail->subject('Hello!')
128+
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
129+
->from(new Address('fabpot@symfony.com', 'Fabien'))
130+
->text('Hello here!')
131+
->html('Hello there!')
132+
->addCc('foo@bar.fr')
133+
->addBcc('foo@bar.fr')
134+
->addReplyTo('foo@bar.fr')
135+
->attachPart($dataPart)
136+
;
137+
138+
$message = $transport->send($mail);
139+
140+
$this->assertSame('foobar', $message->getMessageId());
141+
}
142+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Symfony\Component\Mailer\Bridge\Sendinblue\Tests\Transport;
4+
5+
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueApiTransport;
6+
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueSmtpsTransport;
7+
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueSmtpTransport;
8+
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
9+
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
10+
use Symfony\Component\Mailer\Transport\Dsn;
11+
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
12+
13+
class SendinblueTransportFactoryTest extends TransportFactoryTestCase
14+
{
15+
public function getFactory(): TransportFactoryInterface
16+
{
17+
return new SendinblueTransportFactory($this->getDispatcher(), $this->getClient(), $this->getLogger());
18+
}
19+
20+
public function supportsProvider(): iterable
21+
{
22+
yield [
23+
new Dsn('sendinblue', 'default'),
24+
true,
25+
];
26+
27+
yield [
28+
new Dsn('sendinblue+smtp', 'default'),
29+
true,
30+
];
31+
32+
yield [
33+
new Dsn('sendinblue+smtps', 'default'),
34+
true,
35+
];
36+
37+
yield [
38+
new Dsn('sendinblue+smtp', 'example.com'),
39+
true,
40+
];
41+
42+
yield [
43+
new Dsn('sendinblue+api', 'default'),
44+
true
45+
];
46+
}
47+
48+
public function createProvider(): iterable
49+
{
50+
yield [
51+
new Dsn('sendinblue', 'default', self::USER, self::PASSWORD),
52+
new SendinblueSmtpTransport(self::USER, self::PASSWORD, $this->getDispatcher(), $this->getLogger()),
53+
];
54+
55+
yield [
56+
new Dsn('sendinblue+smtp', 'default', self::USER, self::PASSWORD),
57+
new SendinblueSmtpTransport(self::USER, self::PASSWORD, $this->getDispatcher(), $this->getLogger()),
58+
];
59+
60+
yield [
61+
new Dsn('sendinblue+smtps', 'default', self::USER, self::PASSWORD),
62+
new SendinblueSmtpsTransport(self::USER, self::PASSWORD, $this->getDispatcher(), $this->getLogger()),
63+
];
64+
65+
yield [
66+
new Dsn('sendinblue+api', 'default', self::USER),
67+
new SendinblueApiTransport(self::USER, $this->getClient(), $this->getDispatcher(), $this->getLogger()),
68+
];
69+
}
70+
71+
public function unsupportedSchemeProvider(): iterable
72+
{
73+
yield [
74+
new Dsn('sendinblue+foo', 'default', self::USER, self::PASSWORD),
75+
'The "sendinblue+foo" scheme is not supported; supported schemes for mailer "sendinblue" are: "sendinblue", "sendinblue+smtp", "sendinblue+smtps", "sendinblue+api".',
76+
];
77+
}
78+
79+
public function incompleteDsnProvider(): iterable
80+
{
81+
yield [new Dsn('sendinblue+smtp', 'default', self::USER)];
82+
83+
yield [new Dsn('sendinblue+smtp', 'default', null, self::PASSWORD)];
84+
85+
yield [new Dsn('sendinblue+api', 'default')];
86+
}
87+
}

0 commit comments

Comments
 (0)
0