mail-craft / mailcraft-php
Official PHP SDK for the MailCraft email API — transactional email, SMTP relay, marketing campaigns, automations, and contact management.
Requires
- php: ^8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- pestphp/pest: ^2.34
- phpstan/phpstan: ^1.11
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Official PHP SDK for the MailCraft email API — transactional email, SMTP relay, marketing campaigns, automations, and contact management for developers and AI agents.
Using Laravel? Use
mail-craft/mailcraft-laravelinstead — it wraps this package with a
Install
composer require mail-craft/mailcraft-php
Usage
use MailCraft\MailCraft; $mailcraft = new MailCraft(getenv('MAILCRAFT_API_KEY')); $mailcraft->emails->send( from: 'hello@yourdomain.com', to: ['person@example.com'], subject: 'Welcome!', html: '<p>Thanks for signing up.</p>', );
Every resource on the client mirrors a section of the API reference:
$mailcraft->emails->send(...); $mailcraft->emails->list(); $mailcraft->emails->get($id); $mailcraft->emails->validate($email); $mailcraft->domains->create(...); $mailcraft->domains->verify($id); $mailcraft->contacts->upsert(...); $mailcraft->contacts->addToLists($id, [$listId]); $mailcraft->contacts->unsubscribe($id); $mailcraft->campaigns->create(...); $mailcraft->campaigns->send($id); $mailcraft->templates->create(...); $mailcraft->templates->update($id, [...]); // saves a new version $mailcraft->metrics->get(...); $mailcraft->metrics->reputation();
See src/MailCraft.php for the full list of resources (domains, senders, contacts, lists, segments, properties, templates, templateFolders, campaigns, webhooks, suppressions, metrics).
Error handling
Every non-2xx response throws a MailCraft\Exceptions\MailCraftApiException:
use MailCraft\Exceptions\MailCraftApiException; try { $mailcraft->emails->send(from: 'hello@yourdomain.com', to: ['bad@example.com'], subject: 'Hi'); } catch (MailCraftApiException $e) { echo $e->status, $e->type, $e->getMessage(); // $e->errors holds field-level validation messages, when present }
Requirements
PHP 8.1+.
License
MIT