-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
[Mailer] Add Azure bridge #52842
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
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 |
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,2 @@ | ||
vendor/ | ||
composer.lock |
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.1 | ||
--- | ||
|
||
* 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) 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. |
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,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) |
128 changes: 128 additions & 0 deletions
128
src/Symfony/Component/Mailer/Bridge/Azure/Tests/Transport/AzureApiTransportTest.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,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.'); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/Symfony/Component/Mailer/Bridge/Azure/Tests/Transport/AzureTransportFactoryTest.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,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)]; | ||
} | ||
} |
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.