PHP code example of nvl / mail-notifications

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

    

nvl / mail-notifications example snippets


use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailable;
use Nvl\MailNotifications\Contracts\TrackableMessage;
use Nvl\MailNotifications\Laravel\Concerns\TracksMailDelivery;
use Nvl\MailNotifications\ValueObjects\TrackingContext;

final class ReceiptMail extends Mailable implements TrackableMessage
{
    use TracksMailDelivery;

    public function trackingContext(): TrackingContext
    {
        return TrackingContext::forCategory('receipt');
    }

    public function envelope(): Envelope
    {
        return new Envelope(subject: 'Receipt');
    }

    public function content(): Content
    {
        return new Content(markdown: 'mail.receipt');
    }
}

Mail::to($recipient)->send(
    (new ReceiptMail)->withoutMailTracking(),
);

'notifiable_types' => [
    'account' => App\Models\Account::class,
],

'extensions' => [
    'provider_adapters' => [
        Nvl\MailNotifications\Adapters\MailerSend\MailerSendAdapter::class,
    ],
    'message_id_resolvers' => [],
    'notifiable_type_providers' => [],
    'scheduled_message_factories' => [
        App\Mail\Scheduled\ReceiptMessageFactory::class,
    ],
    'webhook_managers' => [
        Nvl\MailNotifications\Adapters\MailerSend\MailerSendRemoteWebhookManager::class,
    ],
],

'services' => [
    'tracking_lifecycle' =>
        Nvl\MailNotifications\Services\DatabaseTrackingLifecycle::class,
    'sensitive_data_redactor' =>
        Nvl\MailNotifications\Services\DefaultSensitiveDataRedactor::class,
    'sensitive_storage_transformer' => null,
],

use App\Mail\Scheduled\ReceiptMessageFactory;

'extensions' => [
    // ...
    'scheduled_message_factories' => [
        ReceiptMessageFactory::class,
    ],
],

public function make(ScheduledMessageData $message): Mailable
{
    return (new ReceiptMail($message->payload['receipt_id']))
        ->mailer('mailersend');
}

use Carbon\CarbonImmutable;
use Nvl\MailNotifications\Services\ScheduledMailScheduler;
use Nvl\MailNotifications\ValueObjects\Recipient;
use Nvl\MailNotifications\ValueObjects\ScheduleMailData;
use Nvl\MailNotifications\ValueObjects\ScheduledRecipients;

$scheduled = $scheduler->schedule(new ScheduleMailData(
    factoryAlias: 'receipt',
    payloadVersion: 1,
    payload: ['receipt_id' => $receipt->getKey()],
    recipients: new ScheduledRecipients(
        to: [new Recipient($account->email, $account->name)],
    ),
    scheduledFor: CarbonImmutable::parse('2026-08-01 09:00:00', 'UTC'),
    availableAt: CarbonImmutable::parse('2026-07-29 09:00:00', 'UTC'),
));

$scheduler->reschedule(
    $scheduled->id,
    scheduledFor: $newDeliveryUtc,
    availableAt: $newSubmissionUtc,
);
$scheduler->replacePending($scheduled->id, $replacement);
$scheduler->cancel($scheduled->id);

use Illuminate\Support\Facades\Schedule;

Schedule::command('nvl:mail-notifications:process-scheduled')
    ->everyMinute()
    ->onOneServer()
    ->withoutOverlapping();

Schedule::command('nvl:mail-notifications:recover-scheduled')
    ->everyMinute()
    ->onOneServer()
    ->withoutOverlapping();

use Nvl\MailNotifications\Services\LaravelEncrypterSensitiveDataTransformer;

'services' => [
    // ...
    'sensitive_storage_transformer' =>
        LaravelEncrypterSensitiveDataTransformer::class,
],

'privacy' => [
    // ...
    'sensitive_storage' => [
        'enabled' => true,
        'max_transformed_bytes' => 262_144,
    ],
],

$context = TrackingContext::forCategory('account.password-reset')
    ->forNotifiable($account)
    ->withMetadata(['request_id' => $requestId])
    ->withCorrelation(['reset_record_id' => $resetRecordId]);

use Nvl\MailNotifications\Adapters\MailerSend\MailerSendAdapter;
use Nvl\MailNotifications\Adapters\MailerSend\MailerSendRemoteWebhookManager;

'extensions' => [
    'provider_adapters' => [MailerSendAdapter::class],
    'webhook_managers' => [MailerSendRemoteWebhookManager::class],
],

'providers' => [
    'mailersend' => [
        'mailers' => ['mailersend'],
        'signing_secret' => env(
            'MAIL_NOTIFICATIONS_MAILERSEND_SIGNING_SECRET',
        ),
        // The package 'MAIL_NOTIFICATIONS_MAILERSEND_API_TOKEN'),
            'domain_id' => env('MAIL_NOTIFICATIONS_MAILERSEND_DOMAIN_ID'),
            'api_url' => 'https://api.mailersend.com/v1',
            'timeout_seconds' => 10,
            'connect_timeout_seconds' => 3,
            'pagination' => [
                'page_size' => 100,
                'max_pages' => 10,
            ],
            'webhook' => [
                'name' => 'Mail Notifications',
                'url' => env(
                    'MAIL_NOTIFICATIONS_MAILERSEND_WEBHOOK_URL',
                ),
                'events' => [
                    'activity.sent',
                    'activity.delivered',
                    'activity.deferred',
                    'activity.opened',
                    'activity.opened_unique',
                    'activity.clicked',
                    'activity.clicked_unique',
                    'activity.soft_bounced',
                    'activity.hard_bounced',
                    'activity.unsubscribed',
                    'activity.spam_complaint',
                ],
                'enabled' => true,
                'version' => 2,
            ],
        ],
    ],
],

use DomainException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Nvl\MailNotifications\Exceptions\UnmatchedDeliveryEventException;
use Nvl\MailNotifications\Services\WebhookProcessor;
use Nvl\MailNotifications\ValueObjects\WebhookRequest;
use Throwable;

public function __invoke(
    Request $request,
    WebhookProcessor $webhooks,
): Response {
    try {
        $webhooks->process(
            provider: 'mailersend',
            request: WebhookRequest::fromLaravelRequest(
                provider: 'mailersend',
                request: $request,
            ),
        );
    } catch (UnmatchedDeliveryEventException) {
        return response()->noContent(503);
    } catch (DomainException) {
        return response()->noContent(400);
    } catch (Throwable $exception) {
        report($exception);

        return response()->noContent(503);
    }

    return response()->noContent();
}

use Throwable;

public function failed(?Throwable $exception): void
{
    $this->recordMailTrackingFailure($exception);

    // Host-owned alerting or cleanup...
}

use Nvl\MailNotifications\Models\MailNotification;
use Nvl\MailNotifications\Models\MailNotificationEvent;
use Nvl\MailNotifications\Models\ScheduledMailMessage;

$notification = MailNotification::factory()->delivered()->create();
$event = MailNotificationEvent::factory()
    ->for($notification)
    ->bounced()
    ->create();
$scheduledMessage = ScheduledMailMessage::factory()->processing()->create();
bash
composer migrate
bash
php artisan vendor:publish --tag=mail-notifications-config
php artisan vendor:publish --tag=mail-notifications-skills
php artisan vendor:publish --tag=mail-notifications-mail-views
bash
php artisan vendor:publish --tag=mail-notifications-migrations
php artisan migrate
bash
php artisan nvl:mail-notifications:process-scheduled --limit=50
php artisan nvl:mail-notifications:recover-scheduled --limit=50
bash
php artisan nvl:mail-notifications:prune --dry-run
php artisan nvl:mail-notifications:prune --limit=500
php artisan nvl:mail-notifications:prune \
    --before=2026-06-30T00:00:00Z \
    --limit=500
bash
php artisan nvl:mail-notifications:anonymize --dry-run
php artisan nvl:mail-notifications:anonymize --limit=500
php artisan nvl:mail-notifications:anonymize \
    --before=2026-06-30T00:00:00Z \
    --limit=500
dotenv
MAIL_TESTING_ENABLED=true
[email protected]
MAIL_TESTING_TO_NAME="Mail Preview"
MAIL_TESTING_RESPECT_ENV=true
MAIL_TESTING_ENVIRONMENTS=local,testing,staging
bash
php artisan nvl:mail-notifications:webhooks:sync --provider=mailersend --dry-run
php artisan nvl:mail-notifications:webhooks:sync --provider=mailersend
bash
php artisan nvl:data:types:generate --fail-on-warning
php artisan nvl:data:types:check --fail-on-warning