Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom mail headers #38

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions src/MicrosoftGraphTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\HeaderInterface;
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\MessageConverter;

class MicrosoftGraphTransport extends AbstractTransport
Expand Down Expand Up @@ -40,6 +42,7 @@ protected function doSend(SentMessage $message): void
$html = $email->getHtmlBody();

[$attachments, $html] = $this->prepareAttachments($email, $html);
$headers = $this->toInternetMessageHeaders($email->getHeaders());

$payload = [
'message' => [
Expand All @@ -57,6 +60,9 @@ protected function doSend(SentMessage $message): void
],
'saveToSentItems' => config('mail.mailers.microsoft-graph.save_to_sent_items', false) ?? false,
];
if ($headers !== null) {
$payload['message']['internetMessageHeaders'] = $headers;
}

$this->microsoftGraphApiService->sendMail($envelope->getSender()->getAddress(), $payload);
}
Expand Down Expand Up @@ -112,4 +118,31 @@ 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));
}

/**
* Transforms given Symfony Headers
* Microsoft Graph internet message headers
* @param Headers $headers
* @return array|null
*/
protected function toInternetMessageHeaders(Headers $headers): ?array {
$customHeaders = [];


foreach ($headers->all() as $header) {
$name = $header->getName();
$body = $header->getBodyAsString();

if (isset($name, $body) && str_starts_with($name, 'X-')) {
$customHeaders[] = [
'name' => $name,
'value' => $body,
];
}
}

return count($customHeaders) > 0
? $customHeaders
: null;
}
}
92 changes: 92 additions & 0 deletions tests/MicrosoftGraphTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,95 @@
return true;
});
});


it('sends custom mail headers with microsoft graph', 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',
],
'save_to_sent_items' => null,
]);
Config::set('mail.default', 'microsoft-graph');

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

Http::fake();

Mail::to('[email protected]')
->bcc('[email protected]')
->cc('[email protected]')
->send(new TestMail(includeHeaders: true));

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' => 'HTML',
'content' => '<b>Test</b>'.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,
],
],
'internetMessageHeaders' => [[
'name' => 'X-Custom-Header',
'value' => 'Custom Header'
]]
],
'saveToSentItems' => false
]);

return true;
});
});
18 changes: 16 additions & 2 deletions tests/Stubs/TestMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Headers;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
Expand All @@ -18,7 +19,9 @@ class TestMail extends Mailable
*
* @return void
*/
public function __construct(private readonly bool $isHtml = true) {}
public function __construct(private readonly bool $isHtml = true, private readonly bool $includeHeaders = false)
{
}

/**
* Get the message envelope.
Expand All @@ -39,7 +42,7 @@ public function envelope()
*/
public function content()
{
if (! $this->isHtml) {
if (!$this->isHtml) {
return new Content(text: 'text-mail');
}

Expand All @@ -56,4 +59,15 @@ public function attachments(): array
Attachment::fromPath('tests/Resources/files/test-file-2.txt'),
];
}


public function headers(): Headers
{
if ($this->includeHeaders) {
return new Headers(text: [
'X-Custom-Header' => 'Custom Header',
]);
}
return new Headers();
}
}