victord11 / laravel-msgraph-mail
Laravel Mail driver for Microsoft Office 365 using the MSGraph API
Requires
- php: ^8.1
- illuminate/contracts: ^9.38|^10.0|^11.0|^12.0
- spatie/laravel-package-tools: ^1.14.0
- symfony/mailer: ^6.0|^7.0
Requires (Dev)
- composer/semver: ^3.4
- guzzlehttp/guzzle: ^7.5
- laravel/pint: ^1.0
- nunomaduro/larastan: ^2.0.1|^3.0
- orchestra/testbench: ^7.0|^8.0|^9.0|^10.0
- pestphp/pest: ^1.21|^2.0|^3.0
- pestphp/pest-plugin-laravel: ^1.21|^2.0|^3.0
- phpstan/extension-installer: ^1.1
- spatie/laravel-ray: ^1.26
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-14 11:20:29 UTC
README
This package provides a Microsoft Graph mail driver for Laravel. It is an alternative when you don't want to use the deprecated and unsecure Basic Auth SMTP driver with Microsoft Office 365.
This package it's a fork of the InnoGE/laravel-msgraph-mail package, which is no implemented password grant authentication. It has been improved to support both Client Credentials and Resource Owner Password Credentials authentication methods.
Installation
You can install the package via composer:
composer require victord11/laravel-msgraph-mail
Configuration
Register the Azure App
Microsoft Azure AD Configuration
I have written a detailed Blog Post how you can configure your Microsoft Azure AD Tenant. Sending Mails with Laravel and Microsoft Office 365 the secure way
I want to figure it out on my own
You need to register an Azure App in your Azure AD tenant. You can do this by following the steps in the Microsoft Graph documentation.
After creating the App you have to add the following permissions to the App: Mail.Send (Application permission) you will find it under the "Microsoft Graph" section.
Now you have to Grant Admin Consent for the App. You can do this by following the steps in the Microsoft Graph documentation.
Configuring your Laravel app
First you need to add a new entry to the mail drivers array in your config/mail.php configuration file:
Client Credentials Authentication (Default)
'microsoft-graph' => [ 'transport' => 'microsoft-graph', 'auth_method' => 'client_credentials', '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), ],
Resource Owner Password Credentials Authentication
'microsoft-graph' => [ 'transport' => 'microsoft-graph', 'auth_method' => 'password', 'client_id' => env('MICROSOFT_GRAPH_CLIENT_ID'), 'client_secret' => env('MICROSOFT_GRAPH_CLIENT_SECRET'), 'tenant_id' => env('MICROSOFT_GRAPH_TENANT_ID'), 'username' => env('MICROSOFT_GRAPH_USERNAME'), 'password' => env('MICROSOFT_GRAPH_PASSWORD'), '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.
Note: The client_credentials method is recommended for production use as it's more secure. The password method should only be used when necessary and in secure environments.
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.
Calendar invitations (meeting requests)
Microsoft Graph's JSON sendMail endpoint only accepts a bare media type for file attachments, so the
method=REQUEST parameter of a text/calendar part is dropped and Outlook shows the invitation as a plain
.ics file that can only be imported. To keep meeting requests working the driver can submit the message as
raw MIME instead. This is controlled by the mime_mode option:
'microsoft-graph' => [ // ... 'mime_mode' => env('MAIL_MICROSOFT_GRAPH_MIME_MODE', 'auto'), ],
| Value | Behaviour |
|---|---|
auto (default) |
Messages that contain a text/calendar attachment are sent as MIME, everything else as JSON. |
always |
Every message is sent as MIME. |
never |
Every message is sent as JSON (previous behaviour). |
Attach the calendar with the full content type so that Outlook recognises it as a meeting request:
Attachment::fromData(fn () => $calendar->get(), 'invite.ics') ->withMime('text/calendar; charset=UTF-8; method=REQUEST');
Notes on MIME submissions:
- Graph limits MIME messages to 4 MB.
save_to_sent_itemsis ignored, Exchange always stores the sent copy.- Bcc recipients are passed via the
Bccheader, which Exchange strips before delivery.
Now you can switch your default mail driver to the new microsoft-graph driver by setting the env variable:
MAIL_MAILER=microsoft-graph
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.