-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Mailer] Add MicrosoftGraph API Transport #61290
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
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
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/.gitattributes
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,3 @@ | ||
| /Tests export-ignore | ||
| /phpunit.xml.dist export-ignore | ||
| /.git* export-ignore |
8 changes: 8 additions & 0 deletions
8
...Symfony/Component/Mailer/Bridge/MicrosoftGraph/.github/PULL_REQUEST_TEMPLATE.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/.github/workflows/close-pull-request.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/.gitignore
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,3 @@ | ||
| vendor/ | ||
| composer.lock | ||
| phpunit.xml |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/CHANGELOG.md
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.4 | ||
| --- | ||
|
|
||
| * Add the bridge |
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/LICENSE
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) 2025-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. |
53 changes: 53 additions & 0 deletions
53
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/README.md
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,53 @@ | ||
| Microsoft Graph API Mailer | ||
| ========================== | ||
|
|
||
| Provides [Microsoft Graph API Email](https://learn.microsoft.com/en-us/graph/api/user-sendmail) integration for Symfony Mailer. | ||
|
|
||
| Prerequisites | ||
| ------------- | ||
|
|
||
| You will need to: | ||
| * Register an application in your Microsoft Azure portal, | ||
| * Grant this application the Microsoft Graph `Mail.Send` permission, | ||
| * Create a secret for that app. | ||
|
|
||
| Configuration example | ||
| --------------------- | ||
|
|
||
| ```env | ||
| # MAILER | ||
| MAILER_DSN=microsoftgraph+api://CLIENT_APP_ID:CLIENT_APP_SECRET@default?tenantId=TENANT_ID | ||
| ``` | ||
|
|
||
| This will default to `graph.microsoft.com` for the Graph API and `login.microsoftonline.com` for authentication. | ||
|
|
||
| If you need to use third parties operated or specific regions Microsoft services (China, US Government, etc.), you can specify the Graph Endpoint and the Auth Endpoint explicitly. | ||
|
|
||
| ```env | ||
| # MAILER e.g. for China | ||
| MAILER_DSN=microsoftgraph+api://CLIENT_APP_ID:CLIENT_APP_SECRET@microsoftgraph.chinacloudapi.cn?tenantId=TENANT_ID&authEndpoint=login.partner.microsoftonline.cn | ||
| ``` | ||
|
|
||
| The exact URLs can be found in the Microsoft documentation: | ||
| * [Graph Endpoints](https://learn.microsoft.com/en-us/graph/deployments#microsoft-graph-and-graph-explorer-service-root-endpoints) | ||
| * [Auth Endpoints](https://learn.microsoft.com/en-us/entra/identity-platform/authentication-national-cloud#microsoft-entra-authentication-endpoints) | ||
|
|
||
| You can also specify to not save the messages to sent items using the `noSave` parameter: | ||
|
|
||
| ```env | ||
| # MAILER | ||
| MAILER_DSN=microsoftgraph+api://CLIENT_APP_ID:CLIENT_APP_SECRET@default?tenantId=TENANT_ID&noSave=true | ||
| ``` | ||
|
|
||
| Troubleshooting | ||
| --------------- | ||
|
|
||
| Beware that the sender email address needs to be an address of an account inside your tenant. | ||
|
|
||
| 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) | ||
28 changes: 28 additions & 0 deletions
28
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/Tests/TokenManagerMock.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,28 @@ | ||
| <?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\MicrosoftGraph\Tests; | ||
|
|
||
| use Symfony\Component\HttpClient\MockHttpClient; | ||
| use Symfony\Component\Mailer\Bridge\MicrosoftGraph\TokenManager; | ||
|
|
||
| class TokenManagerMock extends TokenManager | ||
| { | ||
| public function __construct() | ||
| { | ||
| parent::__construct('', '', '', '', '', new MockHttpClient()); | ||
| } | ||
|
|
||
| public function getToken(): string | ||
| { | ||
| return 'ACCESSTOKEN'; | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/Tests/TokenManagerTest.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,92 @@ | ||
| <?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\MicrosoftGraph\Tests; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Clock\Clock; | ||
| use Symfony\Component\Clock\MockClock; | ||
| use Symfony\Component\HttpClient\MockHttpClient; | ||
| use Symfony\Component\HttpClient\Response\JsonMockResponse; | ||
| use Symfony\Component\HttpClient\Response\MockResponse; | ||
| use Symfony\Component\Mailer\Bridge\MicrosoftGraph\TokenManager; | ||
| use Symfony\Component\Mailer\Exception\HttpTransportException; | ||
| use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
|
||
| class TokenManagerTest extends TestCase | ||
| { | ||
| public function testTokenRetrieved() | ||
| { | ||
| $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
| $this->assertSame('POST', $method); | ||
| $this->assertSame('https://auth/tenant/oauth2/v2.0/token', $url); | ||
|
|
||
| parse_str($options['body'], $body); | ||
| $this->assertSame('client', $body['client_id']); | ||
| $this->assertSame('key', $body['client_secret']); | ||
| $this->assertSame('https://graph/.default', $body['scope']); | ||
| $this->assertSame('client_credentials', $body['grant_type']); | ||
|
|
||
| return new JsonMockResponse(['access_token' => 'ACCESSTOKEN', 'expires_in' => 3599]); | ||
| }); | ||
|
|
||
| $manager = new TokenManager('graph', 'auth', 'tenant', 'client', 'key', $client); | ||
|
|
||
| $manager->getToken(); | ||
| } | ||
|
|
||
| public function testTokenCached() | ||
| { | ||
| $counter = 0; | ||
| $client = new MockHttpClient(function () use (&$counter): ResponseInterface { | ||
| ++$counter; | ||
|
|
||
| return new JsonMockResponse(['access_token' => 't'.$counter, 'expires_in' => 3599]); | ||
| }); | ||
|
|
||
| $manager = new TokenManager('graph', 'auth', 'tenant', 'client', 'key', $client); | ||
| $this->assertSame('t1', $manager->getToken()); | ||
| $this->assertSame('t1', $manager->getToken()); | ||
|
|
||
| $this->assertSame(1, $counter); | ||
| } | ||
|
|
||
| public function testTokenExpired() | ||
| { | ||
| $counter = 0; | ||
| $client = new MockHttpClient(function () use (&$counter): ResponseInterface { | ||
| ++$counter; | ||
|
|
||
| return new JsonMockResponse(['access_token' => 't'.$counter, 'expires_in' => 3599]); | ||
| }); | ||
|
|
||
| Clock::set(new MockClock('2025-07-31 11:00')); | ||
| $manager = new TokenManager('graph', 'auth', 'tenant', 'client', 'key', $client); | ||
| $this->assertSame('t1', $manager->getToken()); | ||
| Clock::set(new MockClock('2025-07-31 11:30')); | ||
| $this->assertSame('t1', $manager->getToken()); | ||
| Clock::set(new MockClock('2025-07-31 12:00')); | ||
| $this->assertSame('t2', $manager->getToken()); | ||
|
|
||
| $this->assertSame(2, $counter); | ||
| } | ||
|
|
||
| public function testNonSuccessCodeThrown() | ||
| { | ||
| $client = new MockHttpClient(fn (): ResponseInterface => new MockResponse('', ['http_code' => 503])); | ||
|
|
||
| $manager = new TokenManager('graph', 'auth', 'tenant', 'client', 'key', $client); | ||
|
|
||
| $this->expectException(HttpTransportException::class); | ||
| $this->expectExceptionMessageMatches('/^Unable to authenticate/'); | ||
| $manager->getToken(); | ||
| } | ||
| } |
Oops, something went wrong.
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.