PHP SDK
Install the Mailset PHP SDK, send email, verify webhooks and handle errors, with examples that run.
- Every public command238 commands, each a typed method
- No dependenciesNothing else to install beside it
- Typed throughoutReadonly classes, checked by PHPStan
- Retries built inTwo more tries, and a send never doubles
- Timeouts per try30 seconds a try, then tried again
- Idempotency keysA send repeated with its key goes once
On this page
composer require mailset-app/php
Create a client
One client holds one MAILSET_KEY. Make it when your server starts and use it for every call.
Pass the key in
Where your app keeps its secrets elsewhere, hand the key to the client yourself. A token goes the same way.
Options
Everything has a default. Name a credential, an org, how long a try may take and how often a call is tried again, only where yours differ.
<?php
require 'vendor/autoload.php';
// One client for the whole server. It reads MAILSET_KEY
// from the environment; ['key' => …] passes one instead.
// Nothing is sent until you call send.
$mailset = new Mailset\Client();
require 'vendor/autoload.php';
// From your secret store or config, never in the code.
// Or ['token' => …] with an API token.
$key = $config['mailset_key'];
$mailset = new Mailset\Client(['key' => $key]);
<?php
require 'vendor/autoload.php';
use Mailset\Approval;
$mailset = new Mailset\Client([
// Or 'token' => getenv('MAILSET_TOKEN'),
// for an API token.
'key' => (string) getenv('MAILSET_KEY'),
// Where Mailset is. A key names its own, and
// MAILSET_API overrides it.
'baseUrl' => 'https://mailset.app',
// The org commands are about, for a person in
// more than one.
'org' => 'org_7c2e9a4f',
// Each try may take 10 seconds. A network
// failure, a timeout or a 5xx is tried 3 more
Send
Send one message and get its receipt. A message Mailset refuses is an error with a code, never a silent drop.
Give sign-in codes a deadlineHonor unsubscribes and complaintsHandle skipped recipientsGuide for PHP
<?php
require 'vendor/autoload.php';
// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();
$sent = $mailset->send([
'from' => 'Acme <moc.elpmaxe@olleh>',
'to' => 'gro.elpmaxe@ana',
'subject' => 'Your Acme code',
'text' => 'Your code is 123456.',
'html' => '<p>Your code is <b>123456</b>.</p>',
'deliverBy' => '10m',
]);
echo $sent->id, ' ', $sent->status, PHP_EOL;
Send many
Send several messages in one call and get one result per message, in the same order. A refused message never stops the rest.
<?php
require 'vendor/autoload.php';
use Mailset\Sent;
// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();
$results = $mailset->sendMany([
[
'from' => 'Acme <moc.elpmaxe@olleh>',
'to' => 'gro.elpmaxe@ana',
'subject' => 'Your invoice',
'text' => 'Invoice 8842 is ready.',
],
[
'from' => 'Acme <moc.elpmaxe@olleh>',
Promotional email
Send a newsletter or an offer with mode: 'promotional'. {{mailset_unsubscribe}} is a placeholder Mailset fills in for each message, with a signed one-click unsubscribe link unique to that recipient and that message. You never generate the link, and Mailset adds the matching List-Unsubscribe and List-Unsubscribe-Post headers itself.
<?php
require 'vendor/autoload.php';
// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();
$text = <<<'TEXT'
Three new things this month.
Leave this list: {{mailset_unsubscribe}}
TEXT;
$html = <<<'HTML'
<p>Three new things this month.</p>
<a href="{{mailset_unsubscribe}}">Leave this list</a>
HTML;
List-Unsubscribe: <https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1Njc> List-Unsubscribe-Post: List-Unsubscribe=One-Click
Three new things this month.
Leave this list: https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcThree new things this month.
Leave this list
A sample link. Mailset makes a new one for every message.
Three new things this month.
Unsubscribe: https://mailset.app/v1/unsubscribe/REjwEgpOG_zgHOqoFABR-wIAnGDtBkNRV-G7ho9WVWEYl8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcKYW5hQGV4YW1wbGUub3JnCmRfMDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3Cm91dF8wMTIzNDU2Nzg5YWJjZGVmMDEyMzQ1NjcThree new things this month.
Unsubscribe
Mailset adds the last line of each part. The headers are the same.
Idempotency
Name a send with idempotencyKey, and sending it again after a timeout or a crash can’t send it twice.
<?php
require 'vendor/autoload.php';
// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();
$receipt = [
'from' => 'Acme <moc.elpmaxe@olleh>',
'to' => 'gro.elpmaxe@ana',
'subject' => 'Your receipt',
'text' => 'Thanks for your order.',
'idempotencyKey' => 'receipt-8842',
];
$first = $mailset->send($receipt);
// After a timeout or a crash, send it again.
// The same key answers the first receipt,
// and nothing goes out twice.
$again = $mailset->send($receipt);
var_dump($first->id === $again->id);
Check the key
Ask what the key may do now. Call it when your server starts and once a day: each call keeps the key alive.
<?php
require 'vendor/autoload.php';
use Mailset\Types\SendingState;
// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();
// When the server starts, and once a day.
$status = $mailset->key->check();
if ($status->paused !== null) {
error_log("Paused: {$status->paused}");
}
$until = intdiv($status->expiresAt, 1000);
echo 'Works until ', gmdate('c', $until), PHP_EOL;
Delivery search
Look up what became of the mail this key sent, one line per recipient. For a support question or a nightly check.
<?php
require 'vendor/autoload.php';
// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();
// Did Ana get her receipt?
$page = $mailset->delivery->search([
'recipient' => 'gro.elpmaxe@ana',
'since' => '2026-11-30',
]);
foreach ($page->lines as $line) {
echo $line->status, ' ', $line->reason, PHP_EOL;
}
// Pass `next` back as `cursor` for the page after.
echo $page->next, PHP_EOL;
Webhooks
Check that a request came from Mailset and read its events. It needs no key and no client, so a service that only receives events uses it alone.
In a web framework
The signature covers the body exactly as it arrived. Read the raw body before anything parses it, and hand that to the check: a body parsed as JSON and written out again no longer matches.
<?php
require 'vendor/autoload.php';
use Mailset\Events\BouncedEvent;
use Mailset\MailsetException;
use Mailset\Webhooks;
try {
$events = Webhooks::verify(
(string) file_get_contents('php://input'),
getallheaders(),
(string) getenv('MAILSET_HOOK_SECRET'),
);
} catch (MailsetException $error) {
http_response_code(401);
exit($error->errorCode);
}
<?php
// routes/api.php: a Laravel route for your hook.
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Mailset\Events\BouncedEvent;
use Mailset\MailsetException;
use Mailset\Webhooks;
Route::post('/mailset', function (Request $request) {
try {
// The signature covers the body as it came:
// read it raw. $request->all() or ->json()
// would lose the bytes it was made over.
$events = Webhooks::verify(
$request->getContent(),
$request->headers,
Errors
Every refusal is one kind of error. Branch on its code, which never changes, and show its message to people.
Retries and timeouts
A network failure or a 5xx answer is tried again, twice unless you set maxRetries, waiting longer each time. One try may take 30 seconds unless you set timeout. A refusal is never tried again: the same request gets the same answer.
<?php
require 'vendor/autoload.php';
use Mailset\Exception\RateLimitException;
use Mailset\MailsetException;
// Reads MAILSET_KEY from the environment.
$mailset = new Mailset\Client();
try {
$mailset->send([
'from' => 'Acme <moc.elpmaxe@olleh>',
'to' => 'gro.elpmaxe@ana',
'subject' => 'Your Acme code',
'text' => 'Your code is 123456.',
'deliverBy' => '10m',
]);
Build the message yourself
Get the message a send would build, without sending it: for a queue of your own, or a call to the API from another service.
<?php
require 'vendor/autoload.php';
$message = Mailset\Message::build([
'from' => 'Acme <moc.elpmaxe@olleh>',
'to' => 'gro.elpmaxe@ana',
'subject' => 'Your Acme code',
'text' => 'Your code is 123456.',
]);
// The `data` that messages.submit takes.
$data = base64_encode($message->raw);
echo $message->messageId, ' ', strlen($data), PHP_EOL;
Version and licence
This page is for version 0.7.0 of the PHP SDK.
You may use the SDK and change it in your own apps and services, to call Mailset. You may not publish it or pass it on, by itself or inside another library.
Something missing, or not working as this page says? Ask us.