An unofficial SDK for Unsend.
On non-2xx responses, methods throw
\Unsend\Exceptions\ApiException
with parsed message and status code.
Install via Composer:
composer require mattstein/unsend-php
Create a client instance with your API key and specify your Unsend domain if you self host:
// Initialize via factory
$unsend = \Unsend\Unsend::create('your-api-key', 'https://app.unsend.dev');
You can then use any of the included methods to interact with the API, where getData()
provides decoded response data and getResponseObject()
provides the entire Guzzle response:
// Send an email
$response = $unsend->sendEmail([
'to' => 'hello@example.tld',
'from' => 'reply@example.tld',
'subject' => 'Library Test Email',
'html' => '<p>This is a test!</p>',
'text' => 'This is a test!'
]);
// Print email ID
echo $response->getData()->emailId;
Available methods follow the API Reference.
Returns a single email record by ID.
$response = $unsend->getEmail('cxbkzjgku000xmw2tg7lndauk');
echo $response->getData()->subject;
Returns an array of email records, optionally filtered by parameters.
$response = $unsend->listEmails([
'domainId' => 3,
]);
foreach ($response->getData()->data as $email) {
echo $email->subject . "\n";
}
Lazy iterator for listEmails()
that continues through pagination instead of returning pagination metadata.
foreach ($unsend->iterateEmails(['domainId' => 3]) as $email) {
echo $email->subject . "\n";
}
Sends an email.
$response = $unsend->sendEmail([
'to' => 'hello@example.tld',
'from' => 'reply@example.tld',
'subject' => 'Library Test Email',
'html' => '<p>This is a test!</p>',
'text' => 'This is a test!'
]);
echo $response->getData()->emailId;
Sends up to 100 emails in one request.
Updates the targeted send time for a scheduled email.
$response = $unsend->sendEmail([
'to' => 'hello@example.tld',
'from' => 'reply@example.tld',
'subject' => 'Library Test Email',
'html' => '<p>This is a test!</p>',
'text' => 'This is a test!',
'scheduledAt' => '2025-06-10T00:00:00Z'
]);
$scheduledEmailId = $response->getData()->emailId;
$unsend->updateSchedule($scheduledEmailId, '2025-06-07T00:00:00Z');
Cancels a scheduled email.
$unsend->cancelSchedule('cxbkzjgku000xmw2tg7lndauk');
Returns a single contact record.
$response = $unsend->getContact(
'cxb19a523000foa3ctrd5h7u7',
'cxb19bmdv000hoa3c3jfpx51t'
);
echo $response->getData()->email;
Returns an array of contact records, optionally filtered by parameters.
$response = $unsend->getContacts('cxb19a523000foa3ctrd5h7u7');
foreach ($response->getData() as $contact) {
echo $contact->email . "\n";
}
Creates a contact record.
$response = $unsend->createContact(
'cxb19a523000foa3ctrd5h7u7',
[
'email' => 'gobiasindustries@example.com',
'firstName' => 'Tobias',
'lastName' => 'Fünke',
'subscribed' => true,
]
);
echo $response->getData()->contactId;
Updates a contact record.
$unsend->updateContact(
'cxb19a523000foa3ctrd5h7u7',
'cxb19bmdv000hoa3c3jfpx51t',
[
'firstName' => 'Surely',
'lastName' => 'Fünke',
]
);
Upserts a contact record.
Deletes a contact record.
$unsend->deleteContact('cxb19a523000foa3ctrd5h7u7', 'cxb19bmdv000hoa3c3jfpx51t');
Returns a single domain record.
Returns an array of domain records.
$response = $unsend->getDomains();
foreach ($response->getData() as $domain) {
echo $domain->name . "\n";
}
Creates a domain record.
$response = $unsend->createDomain(
[
'name' => 'example.com',
'region' => 'us-east-1',
]
);
echo $response->getData()->id;
Attempts to verify a domain.
$response = $unsend->verifyDomain(5);
echo $response->getData()->message;