-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mailer] Add MailerSend bridge #49461
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
40bb194
Added MailerSend mailer bridge
doobas c734cd0
Update tests to use nullable logger and client
doobas e9d702c
Added client mock
doobas 5a0f17a
Merge branch 'symfony:6.3' into mailersend-mailer-bridge
doobas b037aa4
Changes after review
doobas 834b4a7
Use ->toArray() to get response content.
doobas 5e265f4
Catch JsonException
doobas 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
Next
Next commit
Added MailerSend mailer bridge
- Loading branch information
commit 40bb194b48072f42432bfcd4702f5df60a4b106c
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
4 changes: 4 additions & 0 deletions
4
src/Symfony/Component/Mailer/Bridge/MailerSend/.gitattributes
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,2 @@ | ||
vendor/ | ||
composer.lock |
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.3 | ||
--- | ||
|
||
* Add the bridge | ||
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) 2019-present Fabien Potencier | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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,21 @@ | ||
MailerSend Bridge | ||
=============== | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Provides MailerSend integration for Symfony Mailer. | ||
|
||
## Configuration example: | ||
|
||
```env | ||
# API | ||
MAILER_DSN=mailersend+api://$MAILERSEND_API_KEY@default | ||
# SMTP | ||
MAILER_DSN=mailersend+smtp://$MAILERSEND_SMTP_USERNAME:$MAILERSEND_SMTP_PASSWORD@default | ||
``` | ||
|
||
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) |
198 changes: 198 additions & 0 deletions
198
...Symfony/Component/Mailer/Bridge/MailerSend/Tests/Transport/MailerSendApiTransportTest.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,198 @@ | ||
<?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\MailerSend\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendApiTransport; | ||
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 MailerSendApiTransportTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getTransportData | ||
*/ | ||
public function testToString(MailerSendApiTransport $transport, string $expected) | ||
{ | ||
$this->assertSame($expected, (string) $transport); | ||
} | ||
|
||
public static function getTransportData() | ||
{ | ||
yield [ | ||
new MailerSendApiTransport('ACCESS_KEY'), | ||
'mailersend+api://api.mailersend.com', | ||
]; | ||
|
||
yield [ | ||
(new MailerSendApiTransport('ACCESS_KEY'))->setHost('example.com'), | ||
'mailersend+api://example.com', | ||
]; | ||
|
||
yield [ | ||
(new MailerSendApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99), | ||
'mailersend+api://example.com:99', | ||
]; | ||
} | ||
|
||
public function testSendBasicEmail() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
$this->assertSame('POST', $method); | ||
$this->assertSame('https://api.mailersend.com/v1/email', $url); | ||
|
||
$body = json_decode($options['body'], true); | ||
$this->assertSame('test_from@example.com', $body['from']['email']); | ||
$this->assertSame('Test from name', $body['from']['name']); | ||
$this->assertSame('test_to@example.com', $body['to'][0]['email']); | ||
$this->assertSame('Test to name', $body['to'][0]['name']); | ||
$this->assertSame('Test subject', $body['subject']); | ||
$this->assertSame('Lorem ipsum.', $body['text']); | ||
$this->assertSame('<html><body><p>Lorem ipsum.</p></body></html>', $body['html']); | ||
|
||
return new MockResponse('', [ | ||
'http_code' => 202, | ||
'response_headers' => ['x-message-id' => 'test_message_id'], | ||
]); | ||
}); | ||
|
||
$transport = new MailerSendApiTransport('ACCESS_KEY', $client); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Test subject') | ||
->to(new Address('test_to@example.com', 'Test to name')) | ||
->from(new Address('test_from@example.com', 'Test from name')) | ||
->addCc('test_cc@example.com') | ||
->addBcc('test_bcc@example.com') | ||
->addReplyTo('test_reply_to@example.com') | ||
->text('Lorem ipsum.') | ||
->html('<html><body><p>Lorem ipsum.</p></body></html>'); | ||
|
||
$message = $transport->send($mail); | ||
|
||
$this->assertSame('test_message_id', $message->getMessageId()); | ||
} | ||
|
||
public function testSendEmailWithAttachment() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
$this->assertSame('POST', $method); | ||
$this->assertSame('https://api.mailersend.com/v1/email', $url); | ||
|
||
$body = json_decode($options['body'], true); | ||
|
||
$this->assertSame('content', base64_decode($body['attachments'][0]['content'])); | ||
$this->assertSame('attachment.txt', $body['attachments'][0]['filename']); | ||
$this->assertSame('inline content', base64_decode($body['attachments'][1]['content'])); | ||
$this->assertSame('inline.txt', $body['attachments'][1]['filename']); | ||
$this->assertSame('inline', $body['attachments'][1]['disposition']); | ||
$this->assertSame('test_cid@symfony', $body['attachments'][1]['id']); | ||
|
||
return new MockResponse('', [ | ||
'http_code' => 202, | ||
'response_headers' => ['x-message-id' => 'test_message_id'], | ||
]); | ||
}); | ||
|
||
$transport = new MailerSendApiTransport('ACCESS_KEY', $client); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Test subject') | ||
->to(new Address('test_to@example.com', 'Test to name')) | ||
->from(new Address('test_from@example.com', 'Test from name')) | ||
->addCc('test_cc@example.com') | ||
->addBcc('test_bcc@example.com') | ||
->addReplyTo('test_reply_to@example.com') | ||
->html('<html><body><p>Lorem ipsum.</p><img src="cid:test_cid@symfony"></body></html>') | ||
->addPart(new DataPart('content', 'attachment.txt', 'text/plain')) | ||
->addPart((new DataPart('inline content', 'inline.txt', 'text/plain'))->asInline()->setContentId('test_cid@symfony')); | ||
|
||
$message = $transport->send($mail); | ||
|
||
$this->assertSame('test_message_id', $message->getMessageId()); | ||
} | ||
|
||
public function testSendThrowsForErrorResponse() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
return new MockResponse(json_encode(['message' => 'i\'m a teapot']), [ | ||
'http_code' => 418, | ||
]); | ||
}); | ||
|
||
10000 $transport = new MailerSendApiTransport('ACCESS_KEY', $client); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Test subject') | ||
->to(new Address('test_to@example.com', 'Test to name')) | ||
->from(new Address('test_from@example.com', 'Test from name')) | ||
->text('Lorem ipsum.'); | ||
|
||
$this->expectException(HttpTransportException::class); | ||
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).'); | ||
$transport->send($mail); | ||
} | ||
|
||
public function testSendThrowsForAllSuppressed() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
return new MockResponse(json_encode([ | ||
'message' => 'There are some warnings for your request.', | ||
'warnings' => [ | ||
[ | ||
'type' => 'ALL_SUPPRESSED', | ||
], | ||
], | ||
], \JSON_THROW_ON_ERROR), [ | ||
'http_code' => 202, | ||
]); | ||
}); | ||
|
||
$transport = new MailerSendApiTransport('ACCESS_KEY', $client); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Test subject') | ||
->to(new Address('test_to@example.com', 'Test to name')) | ||
->from(new Address('test_from@example.com', 'Test from name')) | ||
->text('Lorem ipsum.'); | ||
|
||
$this->expectException(HttpTransportException::class); | ||
$this->expectExceptionMessage('Unable to send an email: There are some warnings for your request.'); | ||
$transport->send($mail); | ||
} | ||
|
||
public function testSendThrowsForBadResponse() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
return new MockResponse('test', [ | ||
'http_code' => 202, | ||
]); | ||
}); | ||
|
||
$transport = new MailerSendApiTransport('ACCESS_KEY', $client); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Test subject') | ||
->to(new Address('test_to@example.com', 'Test to name')) | ||
->from(new Address('test_from@example.com', 'Test from name')) | ||
->text('Lorem ipsum.'); | ||
|
||
$this->expectException(HttpTransportException::class); | ||
$this->expectExceptionMessage('Unable to send an email: "test" (code 202).'); | ||
$transport->send($mail); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...ony/Component/Mailer/Bridge/MailerSend/Tests/Transport/MailerSendTransportFactoryTest.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,90 @@ | ||
<?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\MailerSend\Tests\Transport; | ||
|
||
use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendApiTransport; | ||
use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendSmtpTransport; | ||
use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendTransportFactory; | ||
use Symfony\Component\Mailer\Test\TransportFactoryTestCase; | ||
use Symfony\Component\Mailer\Transport\Dsn; | ||
use Symfony\Component\Mailer\Transport\TransportFactoryInterface; | ||
|
||
class MailerSendTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
public static function getFactory(): TransportFactoryInterface | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return new MailerSendTransportFactory(self::getDispatcher(), self::getClient(), self::getLogger()); | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static function supportsProvider(): iterable | ||
{ | ||
yield [ | ||
new Dsn('mailersend', 'default'), | ||
true, | ||
]; | ||
|
||
yield [ | ||
new Dsn('mailersend+smtp', 'default'), | ||
true, | ||
]; | ||
|
||
yield [ | ||
new Dsn('mailersend+smtp', 'example.com'), | ||
true, | ||
]; | ||
|
||
yield [ | ||
new Dsn('mailersend+api', 'default'), | ||
true, | ||
]; | ||
} | ||
|
||
public static function createProvider(): iterable | ||
{ | ||
yield [ | ||
new Dsn('mailersend', 'default', self::USER, self::PASSWORD), | ||
new MailerSendSmtpTransport(self::USER, self::PASSWORD, self::getDispatcher(), self::getLogger()), | ||
]; | ||
|
||
yield [ | ||
new Dsn('mailersend+smtp', 'default', self::USER, self::PASSWORD), | ||
new MailerSendSmtpTransport(self::USER, self::PASSWORD, self::getDispatcher(), self::getLogger()), | ||
]; | ||
|
||
yield [ | ||
new Dsn('mailersend+smtp', 'default', self::USER, self::PASSWORD, 465), | ||
new MailerSendSmtpTransport(self::USER, self::PASSWORD, self::getDispatcher(), self::getLogger()), | ||
]; | ||
|
||
yield [ | ||
new Dsn('mailersend+api', 'default', self::USER), | ||
new MailerSendApiTransport(self::USER, self::getClient(), self::getDispatcher(), self::getLogger()), | ||
]; | ||
} | ||
|
||
public static function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield [ | ||
new Dsn('mailersend+foo', 'default', self::USER, self::PASSWORD), | ||
'The "mailersend+foo" scheme is not supported; supported schemes for mailer "mailersend" are: "mailersend", "mailersend+smtp", "mailersend+api".', | ||
]; | ||
} | ||
|
||
public static function incompleteDsnProvider(): iterable | ||
{ | ||
yield [new Dsn('mailersend+smtp', 'default', self::USER)]; | ||
|
||
yield [new Dsn('mailersend+smtp', 'default', null, self::PASSWORD)]; | ||
|
||
yield [new Dsn('mailersend+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.
Uh oh!
There was an error while loading. Please reload this page.