PHP code example of posthastemail / laravel-mailer

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

    

posthastemail / laravel-mailer example snippets


Mail::to('[email protected]')->send(new InvoiceIssued($invoice));

use Illuminate\Mail\Mailables\Headers;

public function headers(): Headers
{
    return new Headers(text: [
        'X-Posthaste-Stream' => 'transactional',
        'X-Posthaste-Idempotency-Key' => "invoice-{$this->invoice->id}",
        'X-Posthaste-Schedule-At' => '2026-09-01T09:00:00Z',
    ]);
}

use Posthaste\Laravel\Exceptions\PosthasteException;
use Posthaste\Laravel\Exceptions\RateLimitedException;
use Posthaste\Laravel\Exceptions\SuppressedRecipientException;

public function handle(): void
{
    try {
        Mail::to($this->user)->send(new InvoiceIssued($this->invoice));
    } catch (SuppressedRecipientException $e) {
        // Permanent, and the fix lives in your database.
        $this->user->update(['mailable' => false, 'unmailable_reason' => $e->getReason()]);
    } catch (RateLimitedException $e) {
        // Seconds. Put the job back rather than sleeping a worker.
        $this->release($e->getRetryAfterSeconds() ?? 60);
    } catch (PosthasteException $e) {
        report($e);
        $e->isTransient() ? $this->release(300) : $this->fail($e);
    }
}

use Posthaste\Laravel\Events\MessageAccepted;

Event::listen(function (MessageAccepted $event) {
    foreach ($event->result->suppressedAddresses() as $address) {
        Subscriber::where('email', $address)->update(['mailable' => false]);
    }

    $event->result->id;         // 'msg_AZLm3kQ8T2Sf9pXbNc7HrQ'
    $event->result->duplicate;  // true when an idempotency key replayed
    $event->result->warnings;   // pre-send lint findings that did not withhold it
});

$sent = Mail::to('[email protected]')->send(new InvoiceIssued($invoice));
$sent->getMessageId();  // 'msg_AZLm3kQ8T2Sf9pXbNc7HrQ'

$this->app->bind(\Posthaste\Laravel\Http\HttpTransport::class, MyHttpTransport::class);