Skip to content

Commit

Permalink
Allow individual mailable mail senders
Browse files Browse the repository at this point in the history
  • Loading branch information
geisi committed Nov 28, 2023
1 parent cfe0f62 commit f5ee576
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
3 changes: 1 addition & 2 deletions src/LaravelMsGraphMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
});
}
Expand Down
47 changes: 25 additions & 22 deletions src/MicrosoftGraphTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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];
}

/**
Expand Down Expand Up @@ -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];
}
}
3 changes: 2 additions & 1 deletion src/Services/MicrosoftGraphApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/MicrosoftGraphTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit f5ee576

Please sign in to comment.