Sendery
Sendery

Language SDKs

PHP

Send published Sendery templates from PHP.

Install
composer require sendery/php:^0.1.4
Source code

Requirements

PHP 8.3+ with the cURL extension.

Set up

Choose a published template and create a project API key. Store the key as SENDERY_API_KEY on your server.

Shell
export SENDERY_API_KEY="your_project_api_key"

Send an email

Replace your-template with your published template’s key and data with its variables.

The response contains the email’s id and status. If your template has no variables, pass data: [].

PHP
require 'vendor/autoload.php';

use Sendery\Client;

$sendery = new Client(getenv('SENDERY_API_KEY'));
$receipt = $sendery->send(
    to: '[email protected]',
    template: 'your-template',
    data: ['name' => 'Alex', 'action_url' => 'https://example.com/start'],
);

echo $receipt['id'];

Retrieve an email

Use the returned ID to check delivery status.

PHP
$message = $sendery->get($receipt['id']);
echo $message['status'];

Retry a send

Use retry(3) for up to three extra attempts after temporary failures. Keep the same idempotency key and email data on every attempt.

PHP
$email = $sendery->prepare(
    to: '[email protected]',
    template: 'your-template',
    data: ['name' => 'Alex', 'action_url' => 'https://example.com/start'],
    idempotencyKey: 'your-idempotency-key',
);
$receipt = $email->retry(3)->send();

Handle errors

Catch the SDK exception to inspect the status and code. Retry delays are in seconds. The example uses the prepared $email from the retry example above.

PHP
try {
    $receipt = $email->retry(3)->send();
} catch (\Sendery\ApiException $error) {
    error_log($error->status.': '.$error->errorCode);
    // $error->errors contains field-level validation errors.
    // $error->retryAfter is a delay in seconds, when provided.
    throw $error;
}

Send a specific version

Choose a published template version to keep sending it after newer versions are published. By default, Sendery uses the latest version.

PHP
$receipt = $sendery->prepare(
    to: '[email protected]',
    template: 'your-template',
    data: ['name' => 'Alex', 'action_url' => 'https://example.com/start'],
)->version(3)->send();

Attachments

Pass files through attachments. Use Attachment::fromPath() for a local file, or new Attachment() for file contents you already have.

Send up to 10 files totaling 5 MB. See the attachment reference for supported formats and limits.

Example
use Sendery\Attachment;

$sendery->prepare(
    to: '[email protected]',
    template: 'your-template',
    data: ['name' => 'Alex', 'action_url' => 'https://example.com/start'],
    idempotencyKey: 'your-idempotency-key',
    attachments: [
        Attachment::fromPath('/path/document.pdf', 'document.pdf', 'application/pdf'),
    ],
)->retry()->send();