PHP

PHP 8.1+ client. Guzzle-based; resource methods accept and return arrays that mirror the JSON API.

Install

composer require sevk/sevk-php

Initialize

Pass your API key to new Sevk(...).

<?php
require 'vendor/autoload.php';
use Sevk\Sevk;
$sevk = new Sevk('sevk_full_xxxxxxxx');

Send Email

HTML body

<?php
use Sevk\Sevk;
$sevk = new Sevk('sevk_full_xxxxxxxx');
$result = $sevk->emails->send([
'from' => 'sevk <[email protected]>',
'to' => '[email protected]',
'subject' => 'Welcome',
'html' => '<h1>Welcome</h1><p>Thanks for signing up.</p>',
'reply_to' => '[email protected]',
]);
echo $result['id'];

With attachments

$sevk->emails->send([
'from' => 'sevk <[email protected]>',
'to' => '[email protected]',
'subject' => 'Your invoice',
'html' => '<p>Invoice attached.</p>',
'attachments' => [
[
'filename' => 'invoice.pdf',
'content' => $base64Pdf,
'contentType' => 'application/pdf',
],
],
]);

Send Bulk

$result = $sevk->emails->sendBulk([
['from' => 'sevk <[email protected]>', 'to' => '[email protected]', 'subject' => 'Welcome', 'html' => '<p>Hi 1</p>'],
['from' => 'sevk <[email protected]>', 'to' => '[email protected]', 'subject' => 'Welcome', 'html' => '<p>Hi 2</p>'],
]);
echo $result['success'] . ' / ' . $result['failed'];

Contacts

$contact = $sevk->contacts->create(
subscribed: true,
data: ['firstName' => 'John', 'plan' => 'pro']
);
$sevk->contacts->get($contact['id']);
$sevk->contacts->update($contact['id'], data: ['plan' => 'enterprise']);
$page = $sevk->contacts->list(limit: 50);
foreach ($page['items'] as $c) {
echo $c['email'] . PHP_EOL;
}
$sevk->contacts->delete($contact['id']);

Audiences

$audience = $sevk->audiences->create('Newsletter');
$sevk->audiences->addContacts($audience['id'], [
'b2c3d4e5-f678-4901-abcd-ef1234567890',
'b2c3d4e5-f678-4901-abcd-ef1234567891',
]);
foreach ($sevk->audiences->listContacts($audience['id'])['items'] as $c) {
echo $c['email'] . PHP_EOL;
}

Error Handling

Non-2xx responses raise a typed exception from the Sevk\\Exceptions namespace. They all extend SevkException, which exposes getStatusCode(), getErrorCode() and getResponseBody(). The subclass is selected by status: BadRequestException (400), UnauthorizedException (401), ForbiddenException (403), NotFoundException (404), ConflictException (409), RateLimitException (429) and InternalServerErrorException (5xx). A 400 with field errors raises ValidationException.

ValidationException adds getFields() (a map of field name to messages) and RateLimitException adds getRetryAfter() (seconds, or null). Catch the specific types you care about and fall back to SevkException for the rest.

<?php
use Sevk\Sevk;
use Sevk\Exceptions\ValidationException;
use Sevk\Exceptions\RateLimitException;
use Sevk\Exceptions\SevkException;
$sevk = new Sevk('sevk_full_xxxxxxxx');
try {
$sevk->emails->send([
'from' => '[email protected]',
'to' => 'not-an-email',
'subject' => 'Test',
'text' => 'Hi',
]);
} catch (ValidationException $e) {
foreach ($e->getFields() as $field => $messages) {
echo "{$field}: " . implode(', ', $messages) . "\n";
}
} catch (RateLimitException $e) {
echo 'Rate limited, retry after ' . $e->getRetryAfter() . "s\n";
} catch (SevkException $e) {
echo $e->getStatusCode() . ' ' . $e->getErrorCode() . ': ' . $e->getMessage();
}