PHP SDK
Install craftintertech/mailcraft with Composer and send email from PHP 8.1+ — no Guzzle required.
The official SDK for PHP 8.1+. It uses ext-curl (available on almost every host), needs no Guzzle, and is PSR-4 autoloadable — use it with Laravel, Symfony or plain PHP.
Running WordPress? The MailCraft plugin is easier.
Install
composer require craftintertech/mailcraft
Send your first email
<?php
require 'vendor/autoload.php';
use CraftIntertech\MailCraft\MailCraft;
$mailcraft = new MailCraft(getenv('MAILCRAFT_API_KEY'));
$res = $mailcraft->send([
'from' => 'Example Shop <no-reply@mail.example.com>',
'to' => 'customer@example.org',
'subject' => 'Order #10042 confirmed',
'htmlbody' => '<h1>Thanks for your order</h1><p>We are getting it ready.</p>',
'textbody' => 'Thanks for your order. We are getting it ready.',
], ['idempotency_key' => 'order-10042-receipt']);
echo $res['message_id'];
Field names match the REST API. Addresses can be 'Name <a@b.com>' strings or ['address' => ..., 'name' => ...] arrays. Responses are associative arrays.
Attachments
'attachments' => [
['path' => '/var/invoices/INV-001.pdf', 'mime_type' => 'application/pdf'], // read from disk
['name' => 'report.csv', 'data' => $csv], // raw bytes, encoded for you
['name' => 'logo.png', 'content' => $base64], // already base64
],
Templates, scheduling, batches and cancel
$r = $mailcraft->send([
'from' => 'hello@mail.example.com',
'to' => ['address' => 'somchai@example.org', 'name' => 'Somchai'],
'template_alias' => 'welcome',
'template_data' => ['name' => 'Somchai'],
'send_at' => new DateTimeImmutable('tomorrow 09:00', new DateTimeZone('Asia/Bangkok')),
'list_unsubscribe' => true,
]);
$mailcraft->cancel($r['message_id']);
$mailcraft->sendBatch([$message1, $message2]);
$status = $mailcraft->get($r['message_id']);
Errors
Every exception extends MailCraftException with readonly status, errorCode, field, retryAfter and body.
use CraftIntertech\MailCraft\Exception\InsufficientCreditsException;
use CraftIntertech\MailCraft\Exception\MailCraftException;
try {
$mailcraft->send($message);
} catch (InsufficientCreditsException $e) {
error_log("Balance {$e->getBalance()}, required {$e->getRequired()}");
} catch (MailCraftException $e) {
error_log("{$e->status} {$e->errorCode}: {$e->getMessage()}");
}
Use $e->errorCode for the string code — PHP’s own getCode() is always an integer.
Subclasses: ValidationException (400/422), AuthenticationException (401), InsufficientCreditsException (402), PermissionDeniedException (403), NotFoundException (404), ConflictException (409), PayloadTooLargeException (413), RateLimitException (429), ServerException (5xx) and ConnectionException (network failure or timeout).
Automatic retries
Same behaviour as the other SDKs: up to 2 retries on 429 / 5xx / network errors, honouring Retry-After, with an automatic Idempotency-Key so nothing is sent twice.
$mailcraft = new MailCraft($apiKey, [
'timeout' => 30, // seconds
'max_retries' => 2,
'max_retry_delay' => 60,
]);
Verify webhooks
use CraftIntertech\MailCraft\Webhook;
$event = Webhook::parseEvent(
(string) file_get_contents('php://input'),
$_SERVER['HTTP_MAILCRAFT_SIGNATURE'] ?? null,
getenv('MAILCRAFT_WEBHOOK_SECRET'),
);
More (including Laravel) in Verify webhook signatures.