Skip to content

[Mailer] Add MSGraph API Transport #52546

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

Conversation

nguyenk
Copy link

@nguyenk nguyenk commented Nov 10, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues N/A
License MIT

This PR is initially comes from #48888 : Microsoft now forces OAuth for sending emails in SMTP which does not seem possible with the current mailer implementation.

I created a gist for a workaround

And I ended up proposing this PR after @xabbuh proposed

This is a Microsoft Graph API implementation, not an SMTP based.

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "5.4" but it seems your PR description refers to branch "6.4 for features".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title [MAILER] - Add MSGraph API Transpoirt [Mailer] - Add MSGraph API Transpoirt Nov 10, 2023
@carsonbot
Copy link

It looks like you unchecked the "Allow edits from maintainer" box. That is fine, but please note that if you have multiple commits, you'll need to squash your commits into one before this can be merged. Or, you can check the "Allow edits from maintainers" box and the maintainer can squash for you.

Cheers!

Carsonbot

@nguyenk nguyenk changed the base branch from 5.4 to 6.4 November 10, 2023 18:33
@nguyenk nguyenk changed the title [Mailer] - Add MSGraph API Transpoirt [Mailer] - Add MSGraph API Transport Nov 10, 2023
@nguyenk nguyenk force-pushed the mailer-component/add-microsoft-graph-api-mailer-bridge branch from d71c385 to 5dd6b58 Compare November 10, 2023 18:41
@stof
Copy link
Member

stof commented Nov 10, 2023

This cannot go in the 6.4 branch (nor 7.0 btw). We are 1.5 month after the feature freeze.

@nguyenk
Copy link
Author

nguyenk commented Nov 10, 2023

This cannot go in the 6.4 branch (nor 7.0 btw). We are 1.5 month after the feature freeze.

@stof sorry, this is my first contrib. What should I do then ?

@stof
Copy link
Member

stof commented Nov 10, 2023

@nguyenk the 7.1 branch is not yet created to open it to contributions, because we would prefer the community to focus on stabilizing the current beta versions. We create the next branch only once the RC versions are released. So you will need to rebase your work once that happens.

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR also misses the registration of the new bridge in FrameworkBundle and in \Symfony\Component\Mailer\Transport::FACTORY_CLASSES to make it usable through the DSN system.

namespace Symfony\Component\Mailer\Bridge\MicrosoftGraph\Transport;

use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Psr7\Utils;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using guzzle/psr7 without depending on it is a no-go.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it looks like the microsoft graph SDK supports any PSR-7 stream, the transport should rather take a StreamFactory in its constructor.

$tokenRequestContext = new ClientCredentialContext(
$this->tenantId,
$this->clientId,
$this->clientSecret
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those properties used elsewhere than here ? If no, they should not be properties at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I'm wondering whether we should really take those scalars here vs injecting directly the actual dependencies (when using the factory, the instantiation would happen there). This would be more consistent with the way other bridges with dependencies are implemented (see the amazon bridge for instance)

$message->setSubject($source->getSubject() ?? 'No subject');
$itemBody = new ItemBody();
$itemBody->setContent((string) $source->getHtmlBody());
$itemBody->setContentType(new BodyType(BodyType::HTML));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic looks broken in case the Email only has a plaintext body.

use Microsoft\Graph\Generated\Models\Recipient;
use Microsoft\Graph\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you instantiate this class directly which comes from a different package, you should depend on it explicitly instead of relying on the transitive dependency in the microsoft SDK to avoid issues if new major versions are done.

throw new InvalidArgumentException(sprintf("Transport 'microsoft+graph' one of these hosts : '%s'", implode(', ', self::CLOUD_MAP)));
}

// This parses the MAILER_DSN containing Microsoft Graph API credentials
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment looks wrong to me.

"require": {
"php": ">=8.1",
"symfony/mailer": "^5.4|^6.4|^7.0",
"thecodingmachine/safe": "^2.5.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this dependency ?

<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks wrong to me, given that our testsuite does not use PHPUnit 10 yet (as our tooling is not yet compatible with it)

private readonly string $tenantId,
private readonly string $clientId,
private readonly string $clientSecret,
private readonly CacheInterface $cache,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cache looks unused.

@nguyenk
Copy link
Author

nguyenk commented Nov 13, 2023

Thank you a lot for you feedback @stof I will have a close look ASAP.

Cheers

@OskarStark OskarStark changed the title [Mailer] - Add MSGraph API Transport [Mailer] Add MSGraph API Transport Nov 13, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 5.4, 7.1 Dec 29, 2023
@pounard
Copy link
Contributor

pounard commented Feb 20, 2024

Hello, this is kind of related, I opened a PR #54013 for a new component proposal which is about managing access tokens (OAuth2, and possibly some others). I think this might interest you @nguyenk since Microsoft access tokens are OAuth2 tokens.

I created this PR after I tried to implement connection to Office 365 / Outlook with the SMTP component, and really think it would worth it to share/aggregate our respective work and patches in order to move in the direction to a more generic, user configurable and re-usable solution.

@stof I know that you are a meticulous, attentive reviewer, I'd very much appreciate your sentiments about this PR as well.

Thanks in advance to all of you.

@JoppeDC
Copy link
Contributor

JoppeDC commented Apr 9, 2024

@nguyenk This seems like a pretty promising bridge! This is also a friendly ping to see how things are going and if you need any help :)

@nguyenk
Copy link
Author

nguyenk commented Apr 23, 2024

Hello @pounard thank you for the hint. I would definitively look into this PR and use it once (I hope) it's merged.

BTW, big 🆙 for you're work on that 👏

@pounard
Copy link
Contributor

pounard commented Apr 23, 2024

Hello @pounard thank you for the hint. I would definitively look into this PR and use it once (I hope) it's merged.

BTW, big 🆙 for you're work on that 👏

Thanks ! It seems it's hard to get traction from core team when you don't have a name in the community, so any help for pushing it forward would be great.

@OskarStark
Copy link
Contributor

Hi, as we are currently in feature freeze for 7.1 there is not so much attention on following topics, sorry for that.

@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@AndreasA
Copy link
Contributor

any news regarding this? will it be in 7.2 or 7.3?

@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@creiner
Copy link
Contributor

creiner commented Apr 14, 2025

Are there any news regarding this PR?
Microsoft annouced the retirement of smtp basic auth in sept 2025.

https://techcommunity.microsoft.com/blog/exchange/exchange-online-to-retire-basic-auth-for-client-submission-smtp-auth/4114750

@OskarStark
Copy link
Contributor

Feel free to take over and finish this PR 👍🏻

@creiner
Copy link
Contributor

creiner commented Apr 15, 2025

If this is ok for @nguyenk I could have a look at it?

@nguyenk
Copy link
Author

nguyenk commented Apr 15, 2025

@creiner thanks for asking. Please do, it's been too long I didn't make any move on this one.

Thank you.

@xabbuh
Copy link
Member

xabbuh commented May 8, 2025

closing here as the PR looks stalled, feel free to reopen when you have time to finish it

@xabbuh xabbuh closed this May 8, 2025
@AndreasA
Copy link
Contributor

AndreasA commented May 9, 2025

@xabbuh it would still be great to get this feature before Symfony 7.4 release as Microsoft will soon remove normal SMTP altogether.

What were the TODOs in this PR and @creiner did you take over the PR / create a new PR for this feature?

@creiner
Copy link
Contributor

creiner commented May 13, 2025

I implemented the comments and opened a new PR #60408 for continuing the work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.