diff --git a/src/LaravelMsGraphMailServiceProvider.php b/src/LaravelMsGraphMailServiceProvider.php index cf132b9..3a55210 100644 --- a/src/LaravelMsGraphMailServiceProvider.php +++ b/src/LaravelMsGraphMailServiceProvider.php @@ -42,8 +42,7 @@ public function boot(): void throw_unless(filled($config['from']['address'] ?? []), ConfigurationMissing::fromAddress()); return new MicrosoftGraphTransport( - app()->make(MicrosoftGraphApiService::class), - $config['from']['address'] + app()->make(MicrosoftGraphApiService::class) ); }); } diff --git a/src/MicrosoftGraphTransport.php b/src/MicrosoftGraphTransport.php index 956cc21..f95caec 100644 --- a/src/MicrosoftGraphTransport.php +++ b/src/MicrosoftGraphTransport.php @@ -16,7 +16,10 @@ class MicrosoftGraphTransport extends AbstractTransport { - public function __construct(protected MicrosoftGraphApiService $microsoftGraphApiService, protected string $from, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null) + public function __construct( + protected MicrosoftGraphApiService $microsoftGraphApiService, + EventDispatcherInterface $dispatcher = null, + LoggerInterface $logger = null) { parent::__construct($dispatcher, $logger); } @@ -55,7 +58,27 @@ protected function doSend(SentMessage $message): void 'saveToSentItems' => config('mail.mailers.microsoft-graph.save_to_sent_items', false), ]; - $this->microsoftGraphApiService->sendMail($this->from, $payload); + $this->microsoftGraphApiService->sendMail($envelope->getSender()->getAddress(), $payload); + } + + protected function prepareAttachments(Email $email, ?string $html): array + { + $attachments = []; + foreach ($email->getAttachments() as $attachment) { + $headers = $attachment->getPreparedHeaders(); + $fileName = $headers->getHeaderParameter('Content-Disposition', 'filename'); + + $attachments[] = [ + '@odata.type' => '#microsoft.graph.fileAttachment', + 'name' => $fileName, + 'contentType' => $attachment->getMediaType(), + 'contentBytes' => base64_encode($attachment->getBody()), + 'contentId' => $fileName, + 'isInline' => $headers->getHeaderBody('Content-Disposition') === 'inline', + ]; + } + + return [$attachments, $html]; } /** @@ -85,24 +108,4 @@ protected function getRecipients(Email $email, Envelope $envelope): Collection return collect($envelope->getRecipients()) ->filter(fn (Address $address) => ! in_array($address, array_merge($email->getCc(), $email->getBcc()), true)); } - - protected function prepareAttachments(Email $email, ?string $html): array - { - $attachments = []; - foreach ($email->getAttachments() as $attachment) { - $headers = $attachment->getPreparedHeaders(); - $fileName = $headers->getHeaderParameter('Content-Disposition', 'filename'); - - $attachments[] = [ - '@odata.type' => '#microsoft.graph.fileAttachment', - 'name' => $fileName, - 'contentType' => $attachment->getMediaType(), - 'contentBytes' => base64_encode($attachment->getBody()), - 'contentId' => $fileName, - 'isInline' => $headers->getHeaderBody('Content-Disposition') === 'inline', - ]; - } - - return [$attachments, $html]; - } } diff --git a/src/Services/MicrosoftGraphApiService.php b/src/Services/MicrosoftGraphApiService.php index c65b20e..b6574ae 100644 --- a/src/Services/MicrosoftGraphApiService.php +++ b/src/Services/MicrosoftGraphApiService.php @@ -10,7 +10,8 @@ class MicrosoftGraphApiService { - public function __construct(protected readonly string $tenantId, + public function __construct( + protected readonly string $tenantId, protected readonly string $clientId, protected readonly string $clientSecret, protected readonly int $accessTokenTtl diff --git a/tests/MicrosoftGraphTransportTest.php b/tests/MicrosoftGraphTransportTest.php index 3192c5f..a5af2dc 100644 --- a/tests/MicrosoftGraphTransportTest.php +++ b/tests/MicrosoftGraphTransportTest.php @@ -400,7 +400,7 @@ Http::assertSent(function (Request $value) { expect($value) - ->url()->toBe('https://graph.microsoft.com/v1.0/users/taylor@laravel.com/sendMail') + ->url()->toBe('https://graph.microsoft.com/v1.0/users/other-mail@laravel.com/sendMail') ->hasHeader('Authorization', 'Bearer foo_access_token')->toBeTrue() ->body()->json()->toBe([ 'message' => [