diff --git a/README.md b/README.md index 5451ab9..789939b 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/LaravelMsGraphMailServiceProvider.php b/src/LaravelMsGraphMailServiceProvider.php index 022bd87..527d4fa 100644 --- a/src/LaravelMsGraphMailServiceProvider.php +++ b/src/LaravelMsGraphMailServiceProvider.php @@ -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); diff --git a/tests/MicrosoftGraphTransportTest.php b/tests/MicrosoftGraphTransportTest.php index 5a3f54e..3e4b107 100644 --- a/tests/MicrosoftGraphTransportTest.php +++ b/tests/MicrosoftGraphTransportTest.php @@ -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', @@ -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' => 'taylor@laravel.com', + 'name' => 'Taylor Otwell', + ]); + + Cache::set('microsoft-graph-api-access-token', 'foo_access_token', 3600); + + Http::fake(); + + Mail::to('caleb@livewire.com') + ->bcc('tim@innoge.de') + ->cc('nuno@laravel.com') + ->send(new TestMail(false)); + + Http::assertSent(function (Request $value) { + expect($value) + ->url()->toBe('https://graph.microsoft.com/v1.0/users/taylor@laravel.com/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' => 'caleb@livewire.com', + ], + ], + ], + 'ccRecipients' => [ + [ + 'emailAddress' => [ + 'address' => 'nuno@laravel.com', + ], + ], + ], + 'bccRecipients' => [ + [ + 'emailAddress' => [ + 'address' => 'tim@innoge.de', + ], + ], + ], + 'replyTo' => [], + 'sender' => [ + 'emailAddress' => [ + 'address' => 'taylor@laravel.com', + ], + ], + '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',