Skip to content

Allow fallback to global from instead of forcing repetition #44

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

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
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,28 @@ First you need to add a new entry to the mail drivers array in your `config/mail
'client_id' => env('MICROSOFT_GRAPH_CLIENT_ID'),
'client_secret' => env('MICROSOFT_GRAPH_CLIENT_SECRET'),
'tenant_id' => env('MICROSOFT_GRAPH_TENANT_ID'),
'from' => [
'address' => env('MAIL_FROM_ADDRESS'),
'name' => env('MAIL_FROM_NAME'),
],
'save_to_sent_items' => env('MAIL_SAVE_TO_SENT_ITEMS', false),
],
```

For the `client_id`, `client_secret` and `tenant_id` you need to use the values from the Azure App you created in the
previous step.
For the `client_id`, `client_secret` and `tenant_id` you need to use the values from the Azure App you created in the previous step.

The `save_to_sent_items` option in Microsoft Graph refers to a parameter that determines whether a sent email should be saved to the sender's "Sent Items" folder within their mailbox.
When this option is set to true, the email will be automatically saved to the "Sent Items" folder, providing a record of the communication.
Conversely, when it's set to false, the email will not be saved to the "Sent Items" folder.
By default, the `save_to_sent_items` option is set to false, which means that emails sent through Microsoft Graph won't be saved in the sender's "Sent Items" folder unless explicitly specified otherwise. This behavior can be useful in scenarios where you might want more control over which emails are saved as sent items, perhaps to reduce clutter or ensure confidentiality.

The `save_to_sent_items` option in Microsoft Graph refers to a parameter that determines whether a sent email should be saved to the sender's "Sent Items" folder within their mailbox. When this option is set to true, the email will be automatically saved to the "Sent Items" folder, providing a record of the communication. Conversely, when it's set to false, the email will not be saved to the "Sent Items" folder.
If you need to override the default sender in `mail.from`, you can add the following to the configuration entry:

By default, the save_to_sent_items option is set to false, which means that emails sent through Microsoft Graph won't be saved in the sender's "Sent Items" folder unless explicitly specified otherwise. This behavior can be useful in scenarios where you might want more control over which emails are saved as sent items, perhaps to reduce clutter or ensure confidentiality.
```php
'microsoft-graph' => [
...,
'from' => [
'address' => env('MAIL_FROM_ADDRESS'),
'name' => env('MAIL_FROM_NAME'),
],
]
```

Now you can switch your default mail driver to the new `microsoft-graph` driver by setting the env variable:

Expand Down
2 changes: 0 additions & 2 deletions src/LaravelMsGraphMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public function configurePackage(Package $package): void
public function boot(): void
{
Mail::extend('microsoft-graph', function (array $config): MicrosoftGraphTransport {
throw_if(blank($config['from']['address'] ?? []), new ConfigurationMissing('from.address'));

$accessTokenTtl = $config['access_token_ttl'] ?? 3000;
if (! is_int($accessTokenTtl)) {
throw new ConfigurationInvalid('access_token_ttl', $accessTokenTtl);
Expand Down
95 changes: 86 additions & 9 deletions tests/MicrosoftGraphTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,6 @@
],
new ConfigurationInvalid('client_secret', null),
],
[
[
'transport' => 'microsoft-graph',
'tenant_id' => 'foo_tenant_id',
'client_id' => 'foo_client_id',
'client_secret' => 'foo_client_secret',
],
new ConfigurationMissing('from.address'),
],
[
[
'transport' => 'microsoft-graph',
Expand Down Expand Up @@ -432,6 +423,92 @@
});
});

test('default from', 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',
]);
Config::set('mail.default', 'microsoft-graph');
Config::set('mail.from', [
'address' => '[email protected]',
'name' => 'Taylor Otwell',
]);

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(false));

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;
});
});

test('the configured mail sender can be overwritten', function () {
Config::set('mail.mailers.microsoft-graph', [
'transport' => 'microsoft-graph',
Expand Down
Loading