Skip to content
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 @@ -2887,6 +2887,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
MailerBridge\Mailomat\Transport\MailomatTransportFactory::class => 'mailer.transport_factory.mailomat',
MailerBridge\MailPace\Transport\MailPaceTransportFactory::class => 'mailer.transport_factory.mailpace',
MailerBridge\Mailchimp\Transport\MandrillTransportFactory::class => 'mailer.transport_factory.mailchimp',
MailerBridge\MicrosoftGraph\Transport\MicrosoftGraphTransportFactory::class => 'mailer.transport_factory.microsoftgraph',
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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\MicrosoftGraph\Transport\MicrosoftGraphTransportFactory;
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 @@ -60,6 +61,7 @@
'mailjet' => MailjetTransportFactory::class,
'mailomat' => MailomatTransportFactory::class,
'mailpace' => MailPaceTransportFactory::class,
'microsoftgraph' => MicrosoftGraphTransportFactory::class,
'native' => NativeTransportFactory::class,
'null' => NullTransportFactory::class,
'postal' => PostalTransportFactory::class,
Expand Down
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
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 src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/LICENSE
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 src/Symfony/Component/Mailer/Bridge/MicrosoftGraph/README.md
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)
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';
}
}
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();
}
}
Loading
Loading