Skip to content

Commit 40f9dff

Browse files
authored
Allow overwriting of email sender (#14)
* Allow individual mailable mail senders
1 parent a211b30 commit 40f9dff

4 files changed

+117
-25
lines changed

src/LaravelMsGraphMailServiceProvider.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public function boot(): void
4141
throw_unless(filled($config['from']['address'] ?? []), ConfigurationMissing::fromAddress());
4242

4343
return new MicrosoftGraphTransport(
44-
app()->make(MicrosoftGraphApiService::class),
45-
$config['from']['address']
44+
app()->make(MicrosoftGraphApiService::class)
4645
);
4746
});
4847
}

src/MicrosoftGraphTransport.php

+25-22
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
class MicrosoftGraphTransport extends AbstractTransport
1818
{
19-
public function __construct(protected MicrosoftGraphApiService $microsoftGraphApiService, protected string $from, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
19+
public function __construct(
20+
protected MicrosoftGraphApiService $microsoftGraphApiService,
21+
EventDispatcherInterface $dispatcher = null,
22+
LoggerInterface $logger = null)
2023
{
2124
parent::__construct($dispatcher, $logger);
2225
}
@@ -55,7 +58,27 @@ protected function doSend(SentMessage $message): void
5558
'saveToSentItems' => config('mail.mailers.microsoft-graph.save_to_sent_items', false) ?? false,
5659
];
5760

58-
$this->microsoftGraphApiService->sendMail($this->from, $payload);
61+
$this->microsoftGraphApiService->sendMail($envelope->getSender()->getAddress(), $payload);
62+
}
63+
64+
protected function prepareAttachments(Email $email, ?string $html): array
65+
{
66+
$attachments = [];
67+
foreach ($email->getAttachments() as $attachment) {
68+
$headers = $attachment->getPreparedHeaders();
69+
$fileName = $headers->getHeaderParameter('Content-Disposition', 'filename');
70+
71+
$attachments[] = [
72+
'@odata.type' => '#microsoft.graph.fileAttachment',
73+
'name' => $fileName,
74+
'contentType' => $attachment->getMediaType(),
75+
'contentBytes' => base64_encode($attachment->getBody()),
76+
'contentId' => $fileName,
77+
'isInline' => $headers->getHeaderBody('Content-Disposition') === 'inline',
78+
];
79+
}
80+
81+
return [$attachments, $html];
5982
}
6083

6184
/**
@@ -85,24 +108,4 @@ protected function getRecipients(Email $email, Envelope $envelope): Collection
85108
return collect($envelope->getRecipients())
86109
->filter(fn (Address $address) => ! in_array($address, array_merge($email->getCc(), $email->getBcc()), true));
87110
}
88-
89-
protected function prepareAttachments(Email $email, ?string $html): array
90-
{
91-
$attachments = [];
92-
foreach ($email->getAttachments() as $attachment) {
93-
$headers = $attachment->getPreparedHeaders();
94-
$fileName = $headers->getHeaderParameter('Content-Disposition', 'filename');
95-
96-
$attachments[] = [
97-
'@odata.type' => '#microsoft.graph.fileAttachment',
98-
'name' => $fileName,
99-
'contentType' => $attachment->getMediaType(),
100-
'contentBytes' => base64_encode($attachment->getBody()),
101-
'contentId' => $fileName,
102-
'isInline' => $headers->getHeaderBody('Content-Disposition') === 'inline',
103-
];
104-
}
105-
106-
return [$attachments, $html];
107-
}
108111
}

src/Services/MicrosoftGraphApiService.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
class MicrosoftGraphApiService
1212
{
13-
public function __construct(protected readonly string $tenantId,
13+
public function __construct(
14+
protected readonly string $tenantId,
1415
protected readonly string $clientId,
1516
protected readonly string $clientSecret,
1617
protected readonly int $accessTokenTtl

tests/MicrosoftGraphTransportTest.php

+89
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,92 @@
373373
return true;
374374
});
375375
});
376+
377+
test('the configured mail sender can be overwritten', function () {
378+
Config::set('mail.mailers.microsoft-graph', [
379+
'transport' => 'microsoft-graph',
380+
'client_id' => 'foo_client_id',
381+
'client_secret' => 'foo_client_secret',
382+
'tenant_id' => 'foo_tenant_id',
383+
'from' => [
384+
'address' => '[email protected]',
385+
'name' => 'Taylor Otwell',
386+
],
387+
]);
388+
Config::set('mail.default', 'microsoft-graph');
389+
390+
Cache::set('microsoft-graph-api-access-token', 'foo_access_token', 3600);
391+
392+
Http::fake();
393+
394+
$mailable = new TestMail(false);
395+
$mailable->from('[email protected]', 'Other Mail');
396+
397+
Mail::to('[email protected]')
398+
399+
400+
->send($mailable);
401+
402+
Http::assertSent(function (Request $value) {
403+
expect($value)
404+
->url()->toBe('https://graph.microsoft.com/v1.0/users/[email protected]/sendMail')
405+
->hasHeader('Authorization', 'Bearer foo_access_token')->toBeTrue()
406+
->body()->json()->toBe([
407+
'message' => [
408+
'subject' => 'Dev Test',
409+
'body' => [
410+
'contentType' => 'Text',
411+
'content' => 'Test'.PHP_EOL,
412+
],
413+
'toRecipients' => [
414+
[
415+
'emailAddress' => [
416+
'address' => '[email protected]',
417+
],
418+
],
419+
],
420+
'ccRecipients' => [
421+
[
422+
'emailAddress' => [
423+
'address' => '[email protected]',
424+
],
425+
],
426+
],
427+
'bccRecipients' => [
428+
[
429+
'emailAddress' => [
430+
'address' => '[email protected]',
431+
],
432+
],
433+
],
434+
'replyTo' => [],
435+
'sender' => [
436+
'emailAddress' => [
437+
'address' => '[email protected]',
438+
],
439+
],
440+
'attachments' => [
441+
[
442+
'@odata.type' => '#microsoft.graph.fileAttachment',
443+
'name' => 'test-file-1.txt',
444+
'contentType' => 'text',
445+
'contentBytes' => 'Zm9vCg==',
446+
'contentId' => 'test-file-1.txt',
447+
'isInline' => false,
448+
],
449+
[
450+
'@odata.type' => '#microsoft.graph.fileAttachment',
451+
'name' => 'test-file-2.txt',
452+
'contentType' => 'text',
453+
'contentBytes' => 'Zm9vCg==',
454+
'contentId' => 'test-file-2.txt',
455+
'isInline' => false,
456+
],
457+
],
458+
],
459+
'saveToSentItems' => false,
460+
]);
461+
462+
return true;
463+
});
464+
});

0 commit comments

Comments
 (0)