Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Mailer] Add MSGraph API Transport#60408

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
creiner wants to merge13 commits intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromcreiner:pr/52546
Open
Show file tree
Hide file tree
Changes from1 commit
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
PrevPrevious commit
NextNext commit
Improved MicrosoftGraphMailer after comments
- added mail text body handling- removed unused properties- added composer deps
  • Loading branch information
@creiner
creiner committedMay 13, 2025
commite086c8f99a649fd5654b6bbe2fc39be9225d545d
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,9 @@
namespace Symfony\Component\Mailer\Bridge\MicrosoftGraph\Tests\Transport;

use Microsoft\Graph\Core\NationalCloud;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Psr\Log\NullLogger;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Mailer\Bridge\MicrosoftGraph\Transport\MicrosoftGraphTransport;
use Symfony\Component\Mailer\Bridge\MicrosoftGraph\Transport\MicrosoftGraphTransportFactory;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
Expand All@@ -26,7 +28,7 @@ class MicrosoftGraphTransportFactoryTest extends TransportFactoryTestCase

public function getFactory(): TransportFactoryInterface
{
return new MicrosoftGraphTransportFactory(newNullAdapter());
return new MicrosoftGraphTransportFactory(null,newMockHttpClient(), new NullLogger());
}

public static function supportsProvider(): iterable
Expand All@@ -46,27 +48,27 @@ public static function createProvider(): iterable
{
yield [
new Dsn('microsoft+graph', 'default', self::USER, self::PASSWORD, null, ['tenant' => self::TENANT]),
new MicrosoftGraphTransport(NationalCloud::GLOBAL, self::TENANT, self::USER, self::PASSWORD, new NullAdapter()),
new MicrosoftGraphTransport(NationalCloud::GLOBAL,new ClientCredentialContext(self::TENANT, self::USER, self::PASSWORD)),
];

yield [
new Dsn('microsoft+graph', 'germany', self::USER, self::PASSWORD, null, ['tenant' => self::TENANT]),
new MicrosoftGraphTransport(NationalCloud::GERMANY, self::TENANT, self::USER, self::PASSWORD, new NullAdapter()),
new MicrosoftGraphTransport(NationalCloud::GERMANY,new ClientCredentialContext(self::TENANT, self::USER, self::PASSWORD)),
];

yield [
new Dsn('microsoft+graph', 'china', self::USER, self::PASSWORD, null, ['tenant' => self::TENANT]),
new MicrosoftGraphTransport(NationalCloud::CHINA, self::TENANT, self::USER, self::PASSWORD, new NullAdapter()),
new MicrosoftGraphTransport(NationalCloud::CHINA,new ClientCredentialContext(self::TENANT, self::USER, self::PASSWORD)),
];

yield [
new Dsn('microsoft+graph', 'us-gov', self::USER, self::PASSWORD, null, ['tenant' => self::TENANT]),
new MicrosoftGraphTransport(NationalCloud::US_GOV, self::TENANT, self::USER, self::PASSWORD, new NullAdapter()),
new MicrosoftGraphTransport(NationalCloud::US_GOV,new ClientCredentialContext(self::TENANT, self::USER, self::PASSWORD)),
];

yield [
new Dsn('microsoft+graph', 'us-dod', self::USER, self::PASSWORD, null, ['tenant' => self::TENANT]),
new MicrosoftGraphTransport(NationalCloud::US_DOD, self::TENANT, self::USER, self::PASSWORD, new NullAdapter()),
new MicrosoftGraphTransport(NationalCloud::US_DOD,new ClientCredentialContext(self::TENANT, self::USER, self::PASSWORD)),
];
}

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,33 +35,24 @@
use Symfony\Component\Mime\Header\ParameterizedHeader;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\RawMessage;
use Symfony\Contracts\Cache\CacheInterface;

class MicrosoftGraphTransport implements TransportInterface
{
private GraphServiceClient $graphServiceClient;

public function __construct(
private readonly string $nationalCloud,
private readonly string $tenantId,
private readonly string $clientId,
private readonly string $clientSecret,
private readonly CacheInterface $cache,
readonly string $nationalCloud,
readonly ClientCredentialContext $clientCredentialContext,
) {
$tokenRequestContext = new ClientCredentialContext(
$this->tenantId,
$this->clientId,
$this->clientSecret
);
$this->graphServiceClient = new GraphServiceClient($tokenRequestContext, [], $this->nationalCloud);
$this->graphServiceClient = new GraphServiceClient($clientCredentialContext, [], $this->nationalCloud);
}

public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
{
$envelope = null !== $envelope ? clone $envelope : Envelope::create($message);

if (!$message instanceof Email) {
throw newSendEmailError(sprintf("This mailer can only handle mails of class '%s' or it's subclasses, instance of %s passed", Email::class, $message::class));
throw newSendMailException(sprintf("This mailer can only handle mails of class '%s' or it's subclasses, instance of %s passed", Email::class, $message::class));
}

$this->sendMail($message);
Expand DownExpand Up@@ -93,12 +84,12 @@ private function convertEmailToGraphMessage(Email $source): Message

// From
if (0 === \count($source->getFrom())) {
throw newSendEmailError("Cannot send mail without 'From'");
throw newSendMailException("Cannot send mail without 'From'");
}

$message->setFrom(self::convertAddressToGraphRecipient($source->getFrom()[0]));

//to
//To
$message->setToRecipients(array_map(
static fn (Address $address) => self::convertAddressToGraphRecipient($address),
$source->getTo()
Expand All@@ -117,12 +108,18 @@ private function convertEmailToGraphMessage(Email $source): Message
));

// Subject & body
$message->setSubject($source->getSubject() ?? 'No subject');
$message->setSubject($source->getSubject() ?? '');

$itemBody = new ItemBody();
$itemBody->setContent((string) $source->getHtmlBody());
$itemBody->setContentType(new BodyType(BodyType::HTML));
$message->setBody($itemBody);
if ($source->getHtmlBody()) {
$itemBody->setContent((string) $source->getHtmlBody());
$itemBody->setContentType(new BodyType(BodyType::HTML));
} else {
$itemBody->setContent((string) $source->getTextBody());
$itemBody->setContentType(new BodyType(BodyType::TEXT));
}

$message->setBody($itemBody);
$message->setAttachments(array_map(
static fn (DataPart $attachment) => self::convertAttachmentGraphAttachment($attachment),
$source->getAttachments()
Expand DownExpand Up@@ -153,10 +150,10 @@ private static function convertAttachmentGraphAttachment(DataPart $source): File
$fileStream = Utils::streamFor($source->bodyToString());
\assert($fileStream instanceof Stream);

$attachment->setContentBytes($fileStream)
->setContentType($source->getMediaType().'/'.$source->getMediaSubtype())
->setName($filename)
->setODataType('#microsoft.graph.fileAttachment');
$attachment->setContentBytes($fileStream);
$attachment->setContentType($source->getMediaType().'/'.$source->getMediaSubtype());
$attachment->setName($filename);
$attachment->setODataType('#microsoft.graph.fileAttachment');

return $attachment;
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,13 @@
namespace Symfony\Component\Mailer\Bridge\MicrosoftGraph\Transport;

use Microsoft\Graph\Core\NationalCloud;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Symfony\Component\Mailer\Exception\IncompleteDsnException;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Contracts\Cache\CacheInterface;

final class MicrosoftGraphTransportFactory extends AbstractTransportFactory
{
Expand All@@ -32,12 +32,6 @@ final class MicrosoftGraphTransportFactory extends AbstractTransportFactory
'us-gov' => NationalCloud::US_GOV,
];

public function __construct(
private readonly CacheInterface $cache,
) {
parent::__construct();
}

/**
* @return string[]
*/
Expand All@@ -59,13 +53,13 @@ public function create(Dsn $dsn): TransportInterface
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
return new MicrosoftGraphTransport(
self::CLOUD_MAP[$dsn->getHost()],
$tenantId,
$this->getUser($dsn),
$this->getPassword($dsn),
$this->cache
new ClientCredentialContext(
$tenantId,
$this->getUser($dsn),
$this->getPassword($dsn)
)
);
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,9 +22,8 @@
"require": {
"php": ">=8.1",
"symfony/mailer": "^5.4|^6.4|^7.0",
"thecodingmachine/safe": "^2.5.0",
"microsoft/microsoft-graph": "^2.0.0",
"symfony/cache": "6.4.x-dev"
"guzzlehttp/psr7": "^2.0"
},
"require-dev": {
"symfony/http-client": "^5.4|^6.4|^7.0"
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
<?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"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp