Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mailer] Add Azure bridge #52842

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
Dec 9, 2023
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 @@ -2559,6 +2559,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
}

$classToServices = [
MailerBridge\Azure\Transport\AzureTransportFactory::class => 'mailer.transport_factory.azure',
MailerBridge\Brevo\Transport\BrevoTransportFactory::class => 'mailer.transport_factory.brevo',
MailerBridge\Google\Transport\GmailTransportFactory::class => 'mailer.transport_factory.gmail',
MailerBridge\Infobip\Transport\InfobipTransportFactory::class => 'mailer.transport_factory.infobip',
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Azure/.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/Azure/.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/Azure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.1
---

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

Provides [Azure Communication Services Email](https://learn.microsoft.com/en-us/azure/communication-services/concepts/email/email-overview) integration for Symfony Mailer.

Configuration example:

```env
# API
MAILER_DSN=azure+api://ACS_RESOURCE_NAME:KEY@default

#API with options

MAILER_DSN=azure+api://ACS_RESOURCE_NAME:KEY@default?api_version=2023-03-31&disable_tracking=false
```

where:
- `ACS_RESOURCE_NAME` is your Azure Communication Services endpoint resource name (https://ACS_RESOURCE_NAME.communication.azure.com)
- `KEY` is your Azure Communication Services Email API Key

Resources
---------

* [Microsoft Azure (ACS) Email API Docs](https://learn.microsoft.com/en-us/rest/api/communication/dataplane/email/send)
* [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,128 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Azure\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\Component\Mailer\Bridge\Azure\Transport\AzureApiTransport;
use Symfony\Component\Mailer\Envelope;
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 AzureApiTransportTest extends TestCase
{
/**
* @dataProvider getTransportData
*/
public function testToString(AzureApiTransport $transport, string $expected)
{
$this->assertSame($expected, (string) $transport);
}

public static function getTransportData(): array
{
return [
[
new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME'),
'azure+api://ACS_RESOURCE_NAME.communication.azure.com',
],
];
}

public function testCustomHeader()
{
$email = new Email();
$email->getHeaders()->addTextHeader('foo', 'bar');
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]')]);

$transport = new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME');
$method = new \ReflectionMethod(AzureApiTransport::class, 'getPayload');
$payload = $method->invoke($transport, $email, $envelope);

$this->assertArrayHasKey('headers', $payload);
$this->assertArrayHasKey('foo', $payload['headers']);
$this->assertEquals('bar', $payload['headers']['foo']);
}

public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://my-acs-resource.communication.azure.com/emails:send?api-version=2023-03-31', $url);

$body = json_decode($options['body'], true);

$message = $body['content'];
$this->assertSame('normal', $body['importance']);
// $this->assertSame('Fabien', $message['from_name']);
$this->assertSame('[email protected]', $body['senderAddress']);
$this->assertSame('Saif Eddin', $body['recipients']['to'][0]['displayName']);
$this->assertSame('[email protected]', $body['recipients']['to'][0]['address']);
$this->assertSame('Hello!', $message['subject']);
$this->assertSame('Hello There!', $message['plainText']);

return new JsonMockResponse([
'id' => 'foobar',
], [
'http_code' => 202,
]);
});

$transport = new AzureApiTransport('KEY', 'my-acs-resource', true, '2023-03-31', $client);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('[email protected]', 'Saif Eddin'))
->from(new Address('[email protected]', 'Fabien'))
->text('Hello There!');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
}

public function testTagAndMetadataHeaders()
{
$email = new Email();
$email->getHeaders()->add(new TagHeader('category-one'));
$email->getHeaders()->add(new MetadataHeader('Color', 'blue'));
$email->getHeaders()->add(new MetadataHeader('Client-ID', '12345'));
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]')]);

$transport = new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME');
$method = new \ReflectionMethod(AzureApiTransport::class, 'getPayload');
$payload = $method->invoke($transport, $email, $envelope);

$this->assertArrayHasKey('headers', $payload);
$this->assertArrayHasKey('X-Tag', $payload['headers']);
$this->assertArrayHasKey('X-Metadata-Color', $payload['headers']);
$this->assertArrayHasKey('X-Metadata-Client-ID', $payload['headers']);

$this->assertCount(3, $payload['headers']);

$this->assertSame('category-one', $payload['headers']['X-Tag']);
$this->assertSame('blue', $payload['headers']['X-Metadata-Color']);
$this->assertSame('12345', $payload['headers']['X-Metadata-Client-ID']);
}

public function testItDoesNotAllowToAddResourceNameWithDot()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Resource name cannot contain or end with a dot');

new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Azure\Tests\Transport;

use Psr\Log\NullLogger;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Mailer\Bridge\Azure\Transport\AzureApiTransport;
use Symfony\Component\Mailer\Bridge\Azure\Transport\AzureTransportFactory;
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;

class AzureTransportFactoryTest extends TransportFactoryTestCase
{
public function getFactory(): TransportFactoryInterface
{
return new AzureTransportFactory(null, new MockHttpClient(), new NullLogger());
}

public static function supportsProvider(): iterable
{
yield [
new Dsn('azure', 'default'),
true,
];

yield [
new Dsn('azure+api', 'default'),
true,
];
}

public static function createProvider(): iterable
{
yield [
new Dsn('azure', 'default', self::USER, self::PASSWORD),
new AzureApiTransport(self::PASSWORD, self::USER, false, '2023-03-31', new MockHttpClient(), null, new NullLogger()),
];
yield [
new Dsn('azure', 'ACS_RESOURCE_NAME', self::USER, self::PASSWORD),
(new AzureApiTransport(self::PASSWORD, self::USER, false, '2023-03-31', new MockHttpClient(), null, new NullLogger()))->setHost('ACS_RESOURCE_NAME'),
];
yield [
new Dsn('azure+api', 'default', self::USER, self::PASSWORD),
new AzureApiTransport(self::PASSWORD, self::USER, false, '2023-03-31', new MockHttpClient(), null, new NullLogger()),
];
yield [
new Dsn('azure+api', 'ACS_RESOURCE_NAME', self::USER, self::PASSWORD),
(new AzureApiTransport(self::PASSWORD, self::USER, false, '2023-03-31', new MockHttpClient(), null, new NullLogger()))->setHost('ACS_RESOURCE_NAME'),
];
}

public static function unsupportedSchemeProvider(): iterable
{
yield [
new Dsn('azure+foo', 'default', self::USER, self::PASSWORD),
'The "azure+foo" scheme is not supported; supported schemes for mailer "azure" are: "azure", "azure+api".',
];
}

public static function incompleteDsnProvider(): iterable
{
yield [new Dsn('azure', 'default')];
yield [new Dsn('azure', 'default', self::USER)];
yield [new Dsn('azure', 'default', null, self::PASSWORD)];
yield [new Dsn('azure+api', 'default')];
yield [new Dsn('azure+api', 'default', self::USER)];
yield [new Dsn('azure+api', 'default', null, self::PASSWORD)];
}
}
Loading