-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Mailer] add Mailtrap bridge #58252
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
[Mailer] add Mailtrap bridge #58252
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,3 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.git* 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 | ||
========= | ||
|
||
7.2 | ||
--- | ||
|
||
* 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) 2024-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,26 @@ | ||
Mailtrap Bridge | ||
=============== | ||
|
||
Provides Mailtrap integration for Symfony Mailer. | ||
|
||
Configuration example: | ||
|
||
```env | ||
# SMTP | ||
MAILER_DSN=mailtrap+smtp://PASSWORD@default | ||
|
||
# API | ||
MAILER_DSN=mailtrap+api://TOKEN@default | ||
``` | ||
|
||
where: | ||
- `PASSWORD` is your Mailtrap SMTP Password | ||
- `TOKEN` is your Mailtrap Server Token | ||
|
||
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) |
150 changes: 150 additions & 0 deletions
150
src/Symfony/Component/Mailer/Bridge/Mailtrap/Tests/Transport/MailtrapApiTransportTest.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,150 @@ | ||
<?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\Mailtrap\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\JsonMockResponse; | ||
use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapApiTransport; | ||
use Symfony\Component\Mailer\Envelope; | ||
use Symfony\Component\Mailer\Exception\HttpTransportException; | ||
use Symfony\Component\Mailer\Exception\TransportException; | ||
use Symfony\Component\Mailer\Header\MetadataHeader; | ||
use Symfony\Component\Mailer\Header\TagHeader; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class MailtrapApiTransportTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getTransportData | ||
*/ | ||
public function testToString(MailtrapApiTransport $transport, string $expected) | ||
{ | ||
$this->assertSame($expected, (string) $transport); | ||
} | ||
|
||
public static function getTransportData(): array | ||
{ | ||
return [ | ||
[ | ||
new MailtrapApiTransport('KEY'), | ||
'mailtrap+api://send.api.mailtrap.io', | ||
], | ||
[ | ||
(new MailtrapApiTransport('KEY'))->setHost('example.com'), | ||
'mailtrap+api://example.com', | ||
], | ||
[ | ||
(new MailtrapApiTransport('KEY'))->setHost('example.com')->setPort(99), | ||
'mailtrap+api://example.com:99', | ||
], | ||
]; | ||
} | ||
|
||
public function testCustomHeader() | ||
{ | ||
$email = new Email(); | ||
$email->getHeaders()->addTextHeader('foo', 'bar'); | ||
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]); | ||
|
||
$transport = new MailtrapApiTransport('ACCESS_KEY'); | ||
$method = new \ReflectionMethod(MailtrapApiTransport::class, 'getPayload'); | ||
$payload = $method->invoke($transport, $email, $envelope); | ||
|
||
$this->assertArrayHasKey('headers', $payload); | ||
$this->assertSame(['foo' => 'bar'], $payload['headers']); | ||
} | ||
|
||
public function testSend() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
$this->assertSame('POST', $method); | ||
$this->assertSame('https://send.api.mailtrap.io/api/send', $url); | ||
|
||
$body = json_decode($options['body'], true); | ||
$this->assertSame(['email' => 'fabpot@symfony.com', 'name' => 'Fabien'], $body['from']); | ||
$this->assertSame([['email' => 'kevin@symfony.com', 'name' => 'Kevin']], $body['to']); | ||
$this->assertSame('Hello!', $body['subject']); | ||
$this->assertSame('Hello There!', $body['text']); | ||
|
||
return new JsonMockResponse([], [ | ||
'http_code' => 200, | ||
]); | ||
}); | ||
|
||
$transport = new MailtrapApiTransport('KEY', $client); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Hello!') | ||
->to(new Address('kevin@symfony.com', 'Kevin')) | ||
->from(new Address('fabpot@symfony.com', 'Fabien')) | ||
->text('Hello There!'); | ||
|
||
$transport->send($mail); | ||
} | ||
|
||
public function testSendThrowsForErrorResponse() | ||
{ | ||
$client = new MockHttpClient(static fn (string $method, string $url, array $options): ResponseInterface => new JsonMockResponse(['errors' => ['i\'m a teapot']], [ | ||
'http_code' => 418, | ||
])); | ||
$transport = new MailtrapApiTransport('KEY', $client); | ||
$transport->setPort(8984); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Hello!') | ||
->to(new Address('kevin@symfony.com', 'Kevin')) | ||
->from(new Address('fabpot@symfony.com', 'Fabien')) | ||
->text('Hello There!'); | ||
|
||
$this->expectException(HttpTransportException::class); | ||
$this->expectExceptionMessage('Unable to send email: "i\'m a teapot" (status code 418).'); | ||
$transport->send($mail); | ||
} | ||
|
||
public function testTagAndMetadataHeaders() | ||
{ | ||
$email = new Email(); | ||
$email->getHeaders()->add(new TagHeader('password-reset')); | ||
$email->getHeaders()->add(new MetadataHeader('Color', 'blue')); | ||
$email->getHeaders()->add(new MetadataHeader('Client-ID', '12345')); | ||
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]); | ||
|
||
$transport = new MailtrapApiTransport('ACCESS_KEY'); | ||
$method = new \ReflectionMethod(MailtrapApiTransport::class, 'getPayload'); | ||
$payload = $method->invoke($transport, $email, $envelope); | ||
|
||
$this->assertArrayNotHasKey('Headers', $payload); | ||
$this->assertArrayHasKey('category', $payload); | ||
$this->assertArrayHasKey('custom_variables', $payload); | ||
|
||
$this->assertSame('password-reset', $payload['category']); | ||
$this->assertSame(['Color' => 'blue', 'Client-ID' => '12345'], $payload['custom_variables']); | ||
} | ||
|
||
public function testMultipleTagsAreNotAllowed() | ||
{ | ||
$email = new Email(); | ||
$email->getHeaders()->add(new TagHeader('tag1')); | ||
$email->getHeaders()->add(new TagHeader('tag2')); | ||
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]); | ||
|
||
$transport = new MailtrapApiTransport('ACCESS_KEY'); | ||
$method = new \ReflectionMethod(MailtrapApiTransport::class, 'getPayload'); | ||
|
||
$this->expectException(TransportException::class); | ||
|
||
$method->invoke($transport, $email, $envelope); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Mailer/Bridge/Mailtrap/Tests/Transport/MailtrapSmtpTransportTest.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,67 @@ | ||
<?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\Mailtrap\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapSmtpTransport; | ||
use Symfony\Component\Mailer\Exception\TransportException; | ||
use Symfony\Component\Mailer\Header\MetadataHeader; | ||
use Symfony\Component\Mailer\Header\TagHeader; | ||
use Symfony\Component\Mime\Email; | ||
|
||
class MailtrapSmtpTransportTest extends TestCase | ||
{ | ||
public function testCustomHeader() | ||
{ | ||
$email = new Email(); | ||
$email->getHeaders()->addTextHeader('foo', 'bar'); | ||
|
||
$transport = new MailtrapSmtpTransport('ACCESS_KEY'); | ||
$method = new \ReflectionMethod(MailtrapSmtpTransport::class, 'addMailtrapHeaders'); | ||
$method->invoke($transport, $email); | ||
|
||
$this->assertCount(1, $email->getHeaders()->toArray()); | ||
$this->assertSame('foo: bar', $email->getHeaders()->get('FOO')->toString()); | ||
} | ||
|
||
public function testTagAndMetadata() | ||
{ | ||
$email = new Email(); | ||
$email->getHeaders()->addTextHeader('foo', 'bar'); | ||
$email->getHeaders()->add(new TagHeader('password-reset')); | ||
$email->getHeaders()->add(new MetadataHeader('Color', 'blue')); | ||
$email->getHeaders()->add(new MetadataHeader('Client-ID', '12345')); | ||
|
||
$transport = new MailtrapSmtpTransport('ACCESS_KEY'); | ||
$method = new \ReflectionMethod(MailtrapSmtpTransport::class, 'addMailtrapHeaders'); | ||
$method->invoke($transport, $email); | ||
|
||
$this->assertCount(3, $email->getHeaders()->toArray()); | ||
$this->assertSame('foo: bar', $email->getHeaders()->get('FOO')->toString()); | ||
$this->assertSame('X-MT-Category: password-reset', $email->getHeaders()->get('X-MT-Category')->toString()); | ||
$this->assertSame('X-MT-Custom-Variables: {"Color":"blue","Client-ID":"12345"}', $email->getHeaders()->get('X-MT-Custom-Variables')->toString()); | ||
} | ||
|
||
public function testMultipleTagsAreNotAllowed() | ||
{ | ||
$email = new Email(); | ||
$email->getHeaders()->add(new TagHeader('tag1')); | ||
$email->getHeaders()->add(new TagHeader('tag2')); | ||
|
||
$transport = new MailtrapSmtpTransport('ACCESS_KEY'); | ||
$method = new \ReflectionMethod(MailtrapSmtpTransport::class, 'addMailtrapHeaders'); | ||
|
||
$this->expectException(TransportException::class); | ||
|
||
$method->invoke($transport, $email); | ||
} | ||
} |
Oops, something went wrong.
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.