PHP code example of holmessohe / graph-mail

1. Go to this page and download the library: Download holmessohe/graph-mail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

holmessohe / graph-mail example snippets


use HolmesSohe\GraphMail\GraphMailService;

$graph = new GraphMailService();

$graph->sendMail(
    to: '[email protected]',
    subject: 'Bonjour',
    htmlBody: '<h1>Bonjour !</h1><p>Ceci est un email de test.</p>'
);

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: '[email protected]',
    subject: 'Confirmation',
    htmlBody: $html
);

use HolmesSohe\GraphMail\GraphMailService;

$graph = new GraphMailService();

$graph->sendMail(
    to: '[email protected]',
    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'
);

use HolmesSohe\GraphMail\GraphMailService;

$graph = new GraphMailService();

$destinataires = [
    '[email protected]',
    '[email protected]',
    '[email protected]',
];

foreach ($destinataires as $email) {
    $graph->sendMail(
        to: $email,
        subject: 'Notification',
        htmlBody: '<p>Bonjour !</p>'
    );
}



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'   => 'est->email,
            subject: $request->subject,
            htmlBody: $html
        );

        return response()->json(['message' => 'Email envoyé avec succès !']);
    }
}



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: '[email protected]',
            subject: 'Nouvelle notification',
            htmlBody: '<p>Une nouvelle action a été effectuée.</p>'
        );

        return response()->json(['message' => 'Notification envoyée !']);
    }
}

use HolmesSohe\GraphMail\GraphMailService;

$graph = new GraphMailService();

try {
    $graph->sendMail(
        to: '[email protected]',
        subject: 'Test',
        htmlBody: '<p>Test</p>'
    );
} catch (\RuntimeException $e) {
    // Gérer l'erreur
    Log::error('Échec envoi email', ['error' => $e->getMessage()]);
}
bash
php artisan vendor:publish --tag=graph-mail-config