PHP code example of darvis / mailtrap

1. Go to this page and download the library: Download darvis/mailtrap 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/ */

    

darvis / mailtrap example snippets


use App\Models\User;
use Illuminate\Support\Facades\Gate;

Gate::define('viewMailtrap', fn (?User $user) => $user?->is_admin === true);

// routes/web.php

use Darvis\Mailtrap\Models\EmailValidation;
use Darvis\Mailtrap\Models\MailLog;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;
use Symfony\Component\Mailer\Exception\TransportException;

Route::get('/mail-demo', function () {
    // Stop all mail to one address (never to the whole domain).
    EmailValidation::markAsBlocked('[email protected]', 'Spam complaint');

    try {
        Mail::raw('Hello', fn ($message) => $message->to('[email protected]')->subject('Demo'));
    } catch (TransportException $e) {
        // "Email address [email protected] is blocked: Spam complaint"
    }

    return ['blocked_logs' => MailLog::toRecipient('[email protected]')->blocked()->count()]; // 1
});
bash
php artisan mailtrap:test [email protected]