Search by

sainisch-sczepanski-streller / laravel-graph-api-mail-driver

sainisch-sczepanski-streller

Send Emails via Microsoft Graph API from your Laravel 10 Project.

Package info

github.com/sainisch-sczepanski-streller/laravel-graph-api-mail-driver

pkg:composer/sainisch-sczepanski-streller/laravel-graph-api-mail-driver

Statistics

Installs: 950

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.1.0 2026-11-26 10:14 UTC

This package is auto-updated.

Last update: 2026-11-26 10:36:43 UTC


README

Send Emails via Microsoft Graph API from your Laravel 10 Project.

Description

Minimal Mail driver to send E-Mails via Microsoft Graph API in Laravel.

Don't forget the app permissions and policies in the Graph API. For further info see: Microsoft Docs

Getting started

install

composer require sainisch-sczepanski-streller/laravel-graph-api-mail-driver

.env

MAIL_FROM_ADDRESS=<your sender address>
MAIL_MAILER=microsoft-graph-api
MAIL_MS_GRAPH_CLIENT_ID=<your client id>
MAIL_MS_GRAPH_CLIENT_SECRET=<your client secret>
MAIL_MS_GRAPH_TENANT_ID=<your tenant id>
MAIL_MS_GRAPH_SAVE_TO_SENT_ITEMS=<bool>

config/mail.php

'mailers' => [

        'microsoft-graph-api' => [
            'transport' => 'microsoft-graph-api',
            'client_id' => env('MAIL_MS_GRAPH_CLIENT_ID'),
            'client_secret' => env('MAIL_MS_GRAPH_CLIENT_SECRET'),
            'tenant_id' => env('MAIL_MS_GRAPH_TENANT_ID'),
            'saveToSentItems' => env('MAIL_MS_GRAPH_SAVE_TO_SENT_ITEMS', true),
            // optional: HTTP timeout in seconds for requests to Microsoft (default 30)
            // 'timeout' => 30,
        ],
...

Usage

Use it like any other Laravel mail driver

 Mail::to([<recipients>])->send(<your mail class>);

Error handling

The driver deliberately stays minimal: it does not retry, back off or rate limit. If sending fails, it logs the response and throws a LaravelGraphApiMailDriver\Exceptions\SendingFailedException carrying what Microsoft answered. Whether and when to retry is up to your application (e.g. a queued job with $tries / backoff()).

Getter Content Source
getStatus(): ?int HTTP status, null on connection errors response status
getErrorCode(): ?string e.g. ErrorInvalidRecipients, ApplicationThrottled JSON body error.code
getErrorMessage(): ?string error message from Microsoft JSON body error.message
getRequestId(): ?string id for Microsoft support header request-id
getRetryAfter(): ?int seconds to wait (429, sometimes 503) header Retry-After

All values are null if Microsoft did not send them (e.g. empty or HTML body on gateway errors). getPrevious() returns the underlying RequestException or ConnectionException.

Cases:

  • Graph responded with an error (4xx/5xx): SendingFailedException with status and, if available, error code, message, request id and Retry-After. On 401 the cached access token is discarded, so the next attempt fetches a new one. The driver does not retry by itself.
  • No connection (DNS, timeout, connection refused): SendingFailedException with status null. Requests time out after 30 seconds (configurable via timeout), so a hanging request does not block a queue worker.
  • Access token could not be requested (e.g. expired client secret, wrong tenant): LaravelGraphApiMailDriver\Exceptions\TokenRequestFailedException, a subclass of SendingFailedException. getErrorCode() / getErrorMessage() contain error / error_description from login.microsoftonline.com, getRequestId() the x-ms-request-id header. Client secret and token are never logged or included in the exception.

The access token is cached per process and renewed 60 seconds before it expires, which also works in long-running queue workers.

use LaravelGraphApiMailDriver\Exceptions\SendingFailedException;
use LaravelGraphApiMailDriver\Exceptions\TokenRequestFailedException;

try {
    Mail::to($recipient)->send(new OrderShipped($order));
} catch (TokenRequestFailedException $e) {
    // configuration / credentials problem, inform IT
} catch (SendingFailedException $e) {
    if ($e->getStatus() === null || $e->getStatus() === 429 || $e->getStatus() >= 500) {
        $this->release($e->getRetryAfter() ?? 60); // transient, try again later
    } else {
        // permanent, e.g. 400 ErrorInvalidRecipients
    }
}

Tests

composer install
composer test

Compatibility

Built for Laravel 10, also works with later versions.

Contributors

License

The MIT License (MIT). Please see license file for more information.