Skip to content

[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 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
MailerBridge\Mailchimp\Transport\MandrillTransportFactory::class => 'mailer.transport_factory.mailchimp',
MailerBridge\Postal\Transport\PostalTransportFactory::class => 'mailer.transport_factory.postal',
MailerBridge\Postmark\Transport\PostmarkTransportFactory::class => 'mailer.transport_factory.postmark',
MailerBridge\Mailtrap\Transport\MailtrapTransportFactory::class => 'mailer.transport_factory.mailtrap',
MailerBridge\Resend\Transport\ResendTransportFactory::class => 'mailer.transport_factory.resend',
MailerBridge\Scaleway\Transport\ScalewayTransportFactory::class => 'mailer.transport_factory.scaleway',
MailerBridge\Sendgrid\Transport\SendgridTransportFactory::class => 'mailer.transport_factory.sendgrid',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailomat\Transport\MailomatTransportFactory;
use Symfony\Component\Mailer\Bridge\MailPace\Transport\MailPaceTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapTransportFactory;
use Symfony\Component\Mailer\Bridge\Postal\Transport\PostalTransportFactory;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
use Symfony\Component\Mailer\Bridge\Resend\Transport\ResendTransportFactory;
Expand Down Expand Up @@ -61,6 +62,7 @@
'null' => NullTransportFactory::class,
'postal' => PostalTransportFactory::class,
'postmark' => PostmarkTransportFactory::class,
'mailtrap' => MailtrapTransportFactory::class,
'resend' => ResendTransportFactory::class,
'scaleway' => ScalewayTransportFactory::class,
'sendgrid' => SendgridTransportFactory::class,
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailtrap/.gitattributes
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailtrap/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailtrap/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.2
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailtrap/LICENSE
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.
26 changes: 26 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailtrap/README.md
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)
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);
}
}
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);
}
}
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy