Skip to content

Commit 9556568

Browse files
committed
minor symfony#61499 [Mailer] use the same transport for both Mailtrap's live and sandbox API (xabbuh)
This PR was merged into the 7.4 branch. Discussion ---------- [Mailer] use the same transport for both Mailtrap's live and sandbox API | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT I agree with symfony#61315 (review) on having distinct DSN schemes for both the live and the sandbox API, but I'd prefer not to increase the maintenance costs for us by using distinct transport classes. Commits ------- 5f577fb use the same transport for both Mailtrap's live and sandbox API
2 parents b614d0a + 5f577fb commit 9556568

File tree

6 files changed

+69
-198
lines changed

6 files changed

+69
-198
lines changed

src/Symfony/Component/Mailer/Bridge/Mailtrap/Tests/Transport/MailtrapApiSandboxTransportTest.php

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/Symfony/Component/Mailer/Bridge/Mailtrap/Tests/Transport/MailtrapApiTransportTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public static function getTransportData(): array
4848
(new MailtrapApiTransport('KEY'))->setHost('example.com')->setPort(99),
4949
'mailtrap+api://example.com:99',
5050
],
51+
[
52+
new MailtrapApiTransport('KEY', null, null, null, 123456),
53+
'mailtrap+sandbox://sandbox.api.mailtrap.io/?inboxId=123456',
54+
],
55+
[
56+
(new MailtrapApiTransport('KEY', null, null, null, 123456))->setHost('example.com'),
57+
'mailtrap+sandbox://example.com/?inboxId=123456',
58+
],
59+
[
60+
(new MailtrapApiTransport('KEY', null, null, null, 123456))->setHost('example.com')->setPort(99),
61+
'mailtrap+sandbox://example.com:99/?inboxId=123456',
62+
],
63+
[
64+
new MailtrapApiTransport('KEY', null, null, null, 123456),
65+
'mailtrap+sandbox://sandbox.api.mailtrap.io/?inboxId=123456',
66+
],
5167
];
5268
}
5369

@@ -65,7 +81,7 @@ public function testCustomHeader()
6581
$this->assertSame(['foo' => 'bar'], $payload['headers']);
6682
}
6783

68-
public function testSend()
84+
public function testSendToLiveApi()
6985
{
7086
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
7187
$this->assertSame('POST', $method);
@@ -93,6 +109,34 @@ public function testSend()
93109
$transport->send($mail);
94110
}
95111

112+
public function testSendToSandboxApi()
113+
{
114+
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
115+
$this->assertSame('POST', $method);
116+
$this->assertSame('https://sandbox.api.mailtrap.io/api/send/123456', $url);
117+
118+
$body = json_decode($options['body'], true);
119+
$this->assertSame(['email' => 'fabpot@symfony.com', 'name' => 'Fabien'], $body['from']);
120+
$this->assertSame([['email' => 'kevin@symfony.com', 'name' => 'Kevin']], $body['to']);
121+
$this->assertSame('Hello!', $body['subject']);
122+
$this->assertSame('Hello There!', $body['text']);
123+
124+
return new JsonMockResponse([], [
125+
'http_code' => 200,
126+
]);
127+
});
128+
129+
$transport = new MailtrapApiTransport('KEY', $client, null, null, 123456);
130+
131+
$mail = new Email();
132+
$mail->subject('Hello!')
133+
->to(new Address('kevin@symfony.com', 'Kevin'))
134+
->from(new Address('fabpot@symfony.com', 'Fabien'))
135+
->text('Hello There!');
136+
137+
$transport->send($mail);
138+
}
139+
96140
public function testSendThrowsForErrorResponse()
97141
{
98142
$client = new MockHttpClient(static fn (string $method, string $url, array $options): ResponseInterface => new JsonMockResponse(['errors' => ['i\'m a teapot']], [

src/Symfony/Component/Mailer/Bridge/Mailtrap/Tests/Transport/MailtrapTransportFactoryTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\NullLogger;
1515
use Symfony\Component\HttpClient\MockHttpClient;
16-
use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapApiSandboxTransport;
1716
use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapApiTransport;
1817
use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapSmtpTransport;
1918
use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapTransportFactory;
@@ -80,12 +79,12 @@ public static function createProvider(): iterable
8079

8180
yield [
8281
new Dsn('mailtrap+sandbox', 'default', self::USER, null, null, ['inboxId' => '123456']),
83-
new MailtrapApiSandboxTransport(self::USER, 123456, new MockHttpClient(), null, $logger),
82+
new MailtrapApiTransport(self::USER, new MockHttpClient(), null, $logger, 123456),
8483
];
8584

8685
yield [
8786
new Dsn('mailtrap+sandbox', 'example.com', self::USER, null, 8080, ['inboxId' => '123456']),
88-
(new MailtrapApiSandboxTransport(self::USER, 123456, new MockHttpClient(), null, $logger))->setHost('example.com')->setPort(8080),
87+
(new MailtrapApiTransport(self::USER, new MockHttpClient(), null, $logger, 123456))->setHost('example.com')->setPort(8080),
8988
];
9089

9190
yield [

src/Symfony/Component/Mailer/Bridge/Mailtrap/Transport/MailtrapApiSandboxTransport.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Symfony/Component/Mailer/Bridge/Mailtrap/Transport/MailtrapApiTransport.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,34 @@
3030
/**
3131
* @author Kevin Bond <kevinbond@gmail.com>
3232
*/
33-
class MailtrapApiTransport extends AbstractApiTransport
33+
final class MailtrapApiTransport extends AbstractApiTransport
3434
{
35-
protected const HOST = 'send.api.mailtrap.io';
35+
private const LIVE_API_HOST = 'send.api.mailtrap.io';
36+
private const SANDBOX_API_HOST = 'sandbox.api.mailtrap.io';
3637
private const HEADERS_TO_BYPASS = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'sender'];
3738

3839
public function __construct(
3940
#[\SensitiveParameter] private string $token,
4041
?HttpClientInterface $client = null,
4142
?EventDispatcherInterface $dispatcher = null,
4243
?LoggerInterface $logger = null,
44+
private ?int $inboxId = null,
4345
) {
4446
parent::__construct($client, $dispatcher, $logger);
4547
}
4648

4749
public function __toString(): string
4850
{
49-
return \sprintf('mailtrap+api://%s%s', $this->host ?: static::HOST, $this->port ? ':'.$this->port : '');
51+
if (null !== $this->inboxId) {
52+
return \sprintf('mailtrap+sandbox://%s/?inboxId=%d', $this->getEndpoint(), $this->inboxId);
53+
}
54+
55+
return \sprintf('mailtrap+api://%s', $this->getEndpoint());
5056
}
5157

5258
protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
5359
{
54-
$response = $this->client->request('POST', 'https://'.$this->getEndpoint(), [
60+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/send'.(null !== $this->inboxId ? '/'.$this->inboxId : ''), [
5561
'json' => $this->getPayload($email, $envelope),
5662
'auth_bearer' => $this->token,
5763
]);
@@ -143,8 +149,16 @@ private static function encodeEmail(Address $address): array
143149
return array_filter(['email' => $address->getEncodedAddress(), 'name' => $address->getName()]);
144150
}
145151

146-
protected function getEndpoint(): string
152+
private function getEndpoint(): string
147153
{
148-
return ($this->host ?: static::HOST).($this->port ? ':'.$this->port : '').'/api/send';
154+
if ($this->host) {
155+
$host = $this->host;
156+
} elseif (null !== $this->inboxId) {
157+
$host = self::SANDBOX_API_HOST;
158+
} else {
159+
$host = self::LIVE_API_HOST;
160+
}
161+
162+
return $host.($this->port ? ':'.$this->port : '');
149163
}
150164
}

src/Symfony/Component/Mailer/Bridge/Mailtrap/Transport/MailtrapTransportFactory.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ public function create(Dsn $dsn): TransportInterface
2929
if ('mailtrap+api' === $scheme || 'mailtrap+sandbox' === $scheme) {
3030
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
3131
$port = $dsn->getPort();
32+
$inboxId = 'mailtrap+sandbox' === $scheme ? $dsn->getOption('inboxId') : null;
3233

33-
if ('mailtrap+api' === $scheme) {
34-
return (new MailtrapApiTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
35-
} else {
36-
return (new MailtrapApiSandboxTransport($user, $dsn->getOption('inboxId'), $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);
37-
}
34+
return (new MailtrapApiTransport($user, $this->client, $this->dispatcher, $this->logger, $inboxId))->setHost($host)->setPort($port);
3835
}
3936

4037
if ('mailtrap+smtp' === $scheme || 'mailtrap+smtps' === $scheme || 'mailtrap' === $scheme) {

0 commit comments

Comments
 (0)