Skip to content

Commit

Permalink
Allow overwriting of email sender (#14)
Browse files Browse the repository at this point in the history
* Allow individual mailable mail senders
  • Loading branch information
geisi authored Nov 28, 2023
1 parent a211b30 commit 40f9dff
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/LaravelMsGraphMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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) ?? 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
89 changes: 89 additions & 0 deletions tests/MicrosoftGraphTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,92 @@
return true;
});
});

test('the configured mail sender can be overwritten', function () {
Config::set('mail.mailers.microsoft-graph', [
'transport' => 'microsoft-graph',
'client_id' => 'foo_client_id',
'client_secret' => 'foo_client_secret',
'tenant_id' => 'foo_tenant_id',
'from' => [
'address' => '[email protected]',
'name' => 'Taylor Otwell',
],
]);
Config::set('mail.default', 'microsoft-graph');

Cache::set('microsoft-graph-api-access-token', 'foo_access_token', 3600);

Http::fake();

$mailable = new TestMail(false);
$mailable->from('[email protected]', 'Other Mail');

Mail::to('[email protected]')
->bcc('[email protected]')
->cc('[email protected]')
->send($mailable);

Http::assertSent(function (Request $value) {
expect($value)
->url()->toBe('https://graph.microsoft.com/v1.0/users/[email protected]/sendMail')
->hasHeader('Authorization', 'Bearer foo_access_token')->toBeTrue()
->body()->json()->toBe([
'message' => [
'subject' => 'Dev Test',
'body' => [
'contentType' => 'Text',
'content' => 'Test'.PHP_EOL,
],
'toRecipients' => [
[
'emailAddress' => [
'address' => '[email protected]',
],
],
],
'ccRecipients' => [
[
'emailAddress' => [
'address' => '[email protected]',
],
],
],
'bccRecipients' => [
[
'emailAddress' => [
'address' => '[email protected]',
],
],
],
'replyTo' => [],
'sender' => [
'emailAddress' => [
'address' => '[email protected]',
],
],
'attachments' => [
[
'@odata.type' => '#microsoft.graph.fileAttachment',
'name' => 'test-file-1.txt',
'contentType' => 'text',
'contentBytes' => 'Zm9vCg==',
'contentId' => 'test-file-1.txt',
'isInline' => false,
],
[
'@odata.type' => '#microsoft.graph.fileAttachment',
'name' => 'test-file-2.txt',
'contentType' => 'text',
'contentBytes' => 'Zm9vCg==',
'contentId' => 'test-file-2.txt',
'isInline' => false,
],
],
],
'saveToSentItems' => false,
]);

return true;
});
});

0 comments on commit 40f9dff

Please sign in to comment.