8000 [Mailer] Add MailerSend bridge by doobas · Pull Request #49461 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added MailerSend mailer bridge
  • Loading branch information
doobas committed Feb 21, 2023
commit 40bb194b48072f42432bfcd4702f5df60a4b106c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
use Symfony\Component\Mailer\Bridge\Google\Transport\GmailTransportFactory;
use Symfony\Component\Mailer\Bridge\Infobip\Transport\InfobipTransportFactory as InfobipMailerTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
use Symfony\Component\Mailer\Bridge\MailPace\Transport\MailPaceTransportFactory;
Expand Down Expand Up @@ -2454,6 +2455,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
$classToServices = [
GmailTransportFactory::class => 'mailer.transport_factory.gmail',
InfobipMailerTransportFactory::class => 'mailer.transport_factory.infobip',
MailerSendTransportFactory::class => 'mailer.transport_factory.mailersend',
MailgunTransportFactory::class => 'mailer.transport_factory.mailgun',
MailjetTransportFactory::class => 'mailer.transport_factory.mailjet',
MailPaceTransportFactory::class => 'mailer.transport_factory.mailpace',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Mailer\Bridge\Google\Transport\GmailTransportFactory;
use Symfony\Component\Mailer\Bridge\Infobip\Transport\InfobipTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
use Symfony\Component\Mailer\Bridge\MailPace\Transport\MailPaceTransportFactory;
Expand Down Expand Up @@ -51,6 +52,10 @@
->parent('mailer.transport_factory.abstract')
->tag('mailer.transport_factory')

->set('mailer.transport_factory.mailersend', MailerSendTransportFactory::class)
->parent('mailer.transport_factory.abstract')
->tag('mailer.transport_factory')

->set('mailer.transport_factory.mailchimp', MandrillTransportFactory::class)
->parent('mailer.transport_factory.abstract')
->tag('mailer.transport_factory')
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/MailerSend/.gitattributes
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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/MailerSend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
7 changes: 7 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/MailerSend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.3
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/MailerSend/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019-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.
21 changes: 21 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/MailerSend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MailerSend Bridge
===============

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)
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);
}
}
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
{
return new MailerSendTransportFactory(self::getDispatcher(), self::getClient(), self::getLogger());
}

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')];
}
}
Loading
0