This package makes it easy to send emails from your personal, work or school account using Microsoft's Graph API, allowing you to benefit from HTTP instead of SMTP with Laravel.
Inspired by wapacro/laravel-msgraph-mail and fixed to work with Laravel 12
Install the package using composer:
composer require paubikas/laravel-msgraph-mailer
Add the configuration to your mail.php config file:
'mailers' => [
'microsoft-graph' => [
'transport' => 'microsoft-graph',
'tenant' => env('MAIL_MSGRAPH_TENANT', 'common'),
'client' => env('MAIL_MSGRAPH_CLIENT'),
'secret' => env('MAIL_MSGRAPH_SECRET')
'saveToSentItems' => env('MAIL_MSGRAPH_SAVE_TO_SENT_ITEMS', false)
]
// ...
]
Add the configuration to your ENV file:
MAIL_MAILER=microsoft-graph
MAIL_MSGRAPH_TENANT=
MAIL_MSGRAPH_CLIENT=
MAIL_MSGRAPH_SECRET=
MAIL_MSGRAPH_SAVE_TO_SENT_ITEMS=false
Valid values for tenant
are your tenant identifier (work & school accounts) or common
for personal accounts.
Note: This package relies on Laravel's Cache interface for caching access tokens. Make sure to configure it properly, too!
To include internal message headers when sending an email via Microsoft Graph, you should use the envelope() function along with the using callback. This allows you to pass custom headers, including internal ones. You can also add other supported headers. For a full list of available headers, refer to the official documentation: Microsoft Graph Message Resource.
Here’s an example of how to add headers:
using: [
function (Email $email) {
$email->getHeaders()->addHeader('isReadReceiptRequested', 'true');
$email->getHeaders()->addHeader('isDeliveryReceiptRequested', 'true');
$email->getHeaders()->addHeader('internetMessageHeassders', json_encode([
'X-Test-ID' => 'TestID',
]));
}
]
To get the necessary client ID and secret you'll need to register your application and grant it the required permissions. Head over to the Azure Portal to do so (you don't need to be an Azure user).
Make sure to grant the Mail.Send permission and to generate a secret afterwards (may be hidden during app registration).
Work & School accounts: Granting your app the Mail.Send permission allows you by default to send emails with every valid email address within your company/school. Use an Exchange Online Application Access Policy to restrict which email addresses are valid senders for your application.