chrisreedio / laravel-mailosaur
Mailosaur SDK for Laravel
Fund package maintenance!
Chris Reed
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- mailosaur/mailosaur: ^7.14
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^2.0||^3.0
- pestphp/pest-plugin-arch: ^2.5||^3.0
- pestphp/pest-plugin-laravel: ^2.0||^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2025-08-12 06:37:13 UTC
README
Thin Laravel integration for the official Mailosaur PHP SDK. This package registers a singleton MailosaurClient
configured from your app’s config/ENV, and exposes it via a simple wrapper class and a facade so you can call the Mailosaur API anywhere in your app.
The PHP usage patterns below are based on the Mailosaur docs: Mailosaur PHP – Find an email.
Requirements
- PHP ^8.3
- Laravel ^10 || ^11 || ^12
Installation
composer require chrisreedio/laravel-mailosaur
Publish the config file:
php artisan vendor:publish --tag="laravel-mailosaur-config"
Set your environment variables (recommended):
MAILOSAUR_API_KEY=your_api_key MAILOSAUR_SERVER_ID=your_server_id # Optional (defaults to "${MAILOSAUR_SERVER_ID}.mailosaur.net") MAILOSAUR_EMAIL_DOMAIN=your-domain.mailosaur.net
The published config config/mailosaur.php
reads from these ENV keys:
return [ 'api_key' => env('MAILOSAUR_API_KEY'), 'server_id' => env('MAILOSAUR_SERVER_ID'), 'domain' => env('MAILOSAUR_EMAIL_DOMAIN', env('MAILOSAUR_SERVER_ID').'.mailosaur.net'), ];
How it works
- The service provider creates a singleton instance of
MailosaurClient
usingconfig('mailosaur.api_key')
. - It wraps the client in
ChrisReedIO\Mailosaur\Mailosaur
, which also exposes yourserverId
anddomain
from config. - A facade alias
Mailosaur
is registered for convenience.
Usage
You can access the same singleton three ways. Choose what fits your style:
- Facade (convenient):
use ChrisReedIO\Mailosaur\Facades\Mailosaur; // or use the alias: use Mailosaur; $client = Mailosaur::client(); $messages = Mailosaur::messages(); $serverId = Mailosaur::serverId(); $domain = Mailosaur::domain();
- Dependency Injection:
use ChrisReedIO\Mailosaur\Mailosaur; public function __construct(private Mailosaur $mailosaur) {} public function __invoke() { $client = $this->mailosaur->client(); }
- Container helper:
$mailosaur = app(\ChrisReedIO\Mailosaur\Mailosaur::class);
Convenience methods (no server ID needed)
These helpers use your configured MAILOSAUR_SERVER_ID
automatically:
// Find a single message matching criteria Mailosaur::getMessage(SearchCriteria $criteria, int $timeout = 10000, ?DateTime $receivedAfter = null, ?string $dir = null): \Mailosaur\Models\Message // Search for messages Mailosaur::searchMessages(SearchCriteria $criteria, int $page = 0, int $itemsPerPage = 50, ?int $timeout = null, ?DateTime $receivedAfter = null, bool $errorOnTimeout = true, ?string $dir = null): \Mailosaur\Models\MessageListResult // List all messages Mailosaur::allMessages(int $page = 0, int $itemsPerPage = 50, ?DateTime $receivedAfter = null, ?string $dir = null): \Mailosaur\Models\MessageListResult // Delete all messages on the configured server Mailosaur::deleteAllMessages(): void // Create a message to a verified email address Mailosaur::createMessage(\Mailosaur\Models\MessageCreateOptions $options): \Mailosaur\Models\Message // Generate a random email address for the configured server Mailosaur::generateEmailAddress(): string
Examples using convenience methods
use ChrisReedIO\Mailosaur\Facades\Mailosaur; use Mailosaur\Models\SearchCriteria; // Get a single matching message $criteria = new SearchCriteria(); $criteria->sentTo = 'anything@' . Mailosaur::domain(); $message = Mailosaur::getMessage($criteria); // Search with timeout and receivedAfter $testStart = new \DateTime(); $result = Mailosaur::searchMessages($criteria, itemsPerPage: 10, timeout: 10_000, receivedAfter: $testStart); // List and delete all $list = Mailosaur::allMessages(itemsPerPage: 10); Mailosaur::deleteAllMessages(); // Generate a random address on the configured server $address = Mailosaur::generateEmailAddress();
Common examples
Find an email (wait for the next matching message)
use ChrisReedIO\Mailosaur\Facades\Mailosaur; use Mailosaur\Models\SearchCriteria; $criteria = new SearchCriteria(); $criteria->sentTo = 'anything@' . Mailosaur::domain(); // Using raw SDK (requires serverId) $message = Mailosaur::messages()->get(Mailosaur::serverId(), $criteria); // Or with convenience method (no serverId needed) // $message = Mailosaur::getMessage($criteria); // e.g. assert subject // expect($message->subject)->toBe('My example email');
Wait with custom timeout and only include messages received after test start
use ChrisReedIO\Mailosaur\Facades\Mailosaur; use Mailosaur\Models\SearchCriteria; $testStart = new \DateTime(); $criteria = new SearchCriteria(); $criteria->sentTo = 'anything@' . Mailosaur::domain(); // timeout in ms, receivedAfter as DateTime $message = Mailosaur::getMessage( criteria: $criteria, timeout: 10_000, receivedAfter: $testStart );
Generate a random email address for the server
use ChrisReedIO\Mailosaur\Facades\Mailosaur; $emailAddress = Mailosaur::generateEmailAddress(); // e.g. "bgwqj@SERVER_ID.mailosaur.net"
Delete all messages on the server
use ChrisReedIO\Mailosaur\Facades\Mailosaur; Mailosaur::deleteAllMessages();
For many more operations (attachments, forwarding, replying, etc.), see the official Mailosaur PHP docs: Mailosaur PHP – Find an email.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.