holmessohe / graph-mail
Send emails via Microsoft Graph API for laravel
v1.0.0
2026-04-09 16:04 UTC
Requires
- php: ^8.0
- illuminate/http: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- laravel/helpers: ^1.8
Requires (Dev)
- laravel/framework: ^11.51
README
Package Laravel pour envoyer des emails via Microsoft Graph API (Office 365 / Azure AD).
Prérequis
- PHP 8.0+
- Laravel 9, 10 ou 11
- Un compte Microsoft Azure avec une application enregistrée
Installation
composer require holmessohe/graph-mail
Configuration
1. Publier la configuration
php artisan vendor:publish --tag=graph-mail-config
Cela va créer le fichier config/graph-mail.php dans ton projet.
2. Ajouter les variables dans .env
MICROSOFT_TENANT_ID=ton-tenant-id MICROSOFT_CLIENT_ID=ton-client-id MICROSOFT_CLIENT_SECRET=ton-client-secret MICROSOFT_SENDER_EMAIL=expediteur@tondomaine.com
3. Où trouver ces valeurs ?
- Va sur portal.azure.com
- App registrations → sélectionne ton application
MICROSOFT_TENANT_ID→ Directory (tenant) IDMICROSOFT_CLIENT_ID→ Application (client) IDMICROSOFT_CLIENT_SECRET→ Certificates & secrets → New client secretMICROSOFT_SENDER_EMAIL→ L'adresse email qui va envoyer les mails
Utilisation
Envoi simple
use HolmesSohe\GraphMail\GraphMailService; $graph = new GraphMailService(); $graph->sendMail( to: 'destinataire@example.com', subject: 'Bonjour', htmlBody: '<h1>Bonjour !</h1><p>Ceci est un email de test.</p>' );
Envoi avec une vue Blade
use HolmesSohe\GraphMail\GraphMailService; use Illuminate\Support\Facades\View; $graph = new GraphMailService(); $html = View::make('emails.mon-template', [ 'nom' => 'Holmès', 'message' => 'Votre demande a bien été reçue.' ])->render(); $graph->sendMail( to: 'destinataire@example.com', subject: 'Confirmation', htmlBody: $html );
Envoi avec pièce jointe (PDF)
use HolmesSohe\GraphMail\GraphMailService; $graph = new GraphMailService(); $graph->sendMail( to: 'destinataire@example.com', subject: 'Votre facture', htmlBody: '<p>Veuillez trouver votre facture en pièce jointe.</p>', attachmentPath: storage_path('app/factures/facture-001.pdf'), attachmentName: 'facture-001.pdf' );
⚠️ La pièce jointe doit faire moins de 3 Mo.
Envoi à plusieurs destinataires
use HolmesSohe\GraphMail\GraphMailService; $graph = new GraphMailService(); $destinataires = [ 'user1@example.com', 'user2@example.com', 'user3@example.com', ]; foreach ($destinataires as $email) { $graph->sendMail( to: $email, subject: 'Notification', htmlBody: '<p>Bonjour !</p>' ); }
Utilisation dans un Controller
<?php namespace App\Http\Controllers; use HolmesSohe\GraphMail\GraphMailService; use Illuminate\Http\Request; use Illuminate\Support\Facades\View; class ContactController extends Controller { public function send(Request $request) { $request->validate([ 'email' => 'required|email', 'subject' => 'required|string', 'message' => 'required|string', ]); $graph = new GraphMailService(); $html = View::make('emails.contact', [ 'data' => $request->all() ])->render(); $graph->sendMail( to: $request->email, subject: $request->subject, htmlBody: $html ); return response()->json(['message' => 'Email envoyé avec succès !']); } }
Injection de dépendance (recommandé)
<?php namespace App\Http\Controllers; use HolmesSohe\GraphMail\GraphMailService; class NotificationController extends Controller { public function __construct( protected GraphMailService $graph ) {} public function notifier() { $this->graph->sendMail( to: 'admin@example.com', subject: 'Nouvelle notification', htmlBody: '<p>Une nouvelle action a été effectuée.</p>' ); return response()->json(['message' => 'Notification envoyée !']); } }
Gestion des erreurs
Le package lance une exception RuntimeException si l'envoi échoue.
Il est recommandé de l'attraper :
use HolmesSohe\GraphMail\GraphMailService; $graph = new GraphMailService(); try { $graph->sendMail( to: 'destinataire@example.com', subject: 'Test', htmlBody: '<p>Test</p>' ); } catch (\RuntimeException $e) { // Gérer l'erreur Log::error('Échec envoi email', ['error' => $e->getMessage()]); }
Méthodes disponibles
sendMail()
| Paramètre | Type | Obligatoire | Description |
|---|---|---|---|
$to |
string |
✅ | Email du destinataire |
$subject |
string |
✅ | Sujet de l'email |
$htmlBody |
string |
✅ | Contenu HTML de l'email |
$attachmentPath |
string|null |
❌ | Chemin vers la pièce jointe |
$attachmentName |
string|null |
❌ | Nom de la pièce jointe |
getAccessToken()
Récupère et met en cache le token OAuth2 pendant 55 minutes. Tu n'as normalement pas besoin d'appeler cette méthode directement.
Licence
MIT — Holmès SOHE