Skip to main content

PHP SDK

PSR-18 compatible, automatic retries, and Laravel integration for SendMesh.

SDK packages will be published to Packagist soon. The API below shows the interface you can expect. Use the REST API directly in the meantime.

Installation

bash
composer require sendmesh/sendmesh-php

Requires PHP 8.1+. PSR-18 HTTP client compatible (Guzzle, Symfony HTTP Client).

Initialization

php
<?php
require_once 'vendor/autoload.php';

use SendMesh\Client;

$client = new Client('sk_live_your_secret_key', [
    // Optional configuration:
    'base_url'    => 'https://api.sendmesh.co',  // default
    'timeout'     => 30,                           // seconds
    'max_retries' => 3,                            // retries on 5xx
]);

Laravel Integration

bash
composer require sendmesh/sendmesh-laravel
env
# .env
SENDMESH_API_KEY=sk_live_your_secret_key
php
// Usage with dependency injection:
use SendMesh\Client;

class EmailController extends Controller
{
    public function send(Client $sendmesh)
    {
        $response = $sendmesh->emails->send([
            'from' => ['email' => 'hello@yourdomain.com'],
            'to' => [['email' => 'user@example.com']],
            'subject' => 'Hello!',
            'html' => '<p>Sent from Laravel</p>',
        ]);

        return response()->json(['emailId' => $response->data->id]);
    }
}

Error Handling

php
use SendMesh\Exceptions\SendMeshException;
use SendMesh\Exceptions\ValidationException;
use SendMesh\Exceptions\RateLimitException;

try {
    $client->emails->send([...]);
} catch (ValidationException $e) {
    echo 'Validation failed: ' . $e->getMessage();
    echo 'Details: ' . json_encode($e->getDetails());
} catch (RateLimitException $e) {
    echo 'Rate limited. Retry after: ' . $e->getRetryAfter() . 's';
} catch (SendMeshException $e) {
    echo 'API error: ' . $e->getCode() . ' ' . $e->getMessage();
    echo 'Status: ' . $e->getStatusCode();
}

Method Reference

POST$client->emails->send()

Send a single email to one or more recipients.

Parameters
php
[
    'from'     => ['email' => string, 'name' => string],    // required
    'to'       => [['email' => string, 'name' => string]],  // required
    'subject'  => string,                                     // required
    'html'     => string|null,
    'text'     => string|null,
    'replyTo'  => string|null,
    'tags'     => string[],
    'metadata' => array,
]
Returns
php
SendResponse
  ->data->id         // string - email ID
  ->data->status     // string - "queued"|"sent"|"delivered"
  ->data->createdAt  // string - ISO timestamp
Example
php
$response = $client->emails->send([
    'from' => ['email' => 'hello@yourdomain.com', 'name' => 'Your App'],
    'to' => [['email' => 'user@example.com', 'name' => 'Jane Doe']],
    'subject' => 'Welcome to Our App!',
    'html' => '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
    'tags' => ['welcome', 'transactional'],
]);

echo 'Email ID: ' . $response->data->id;
POST$client->emails->batch()

Send up to 1,000 emails in a single batch request.

Parameters
php
[
    'from'     => ['email' => string, 'name' => string],
    'messages' => [
        ['to' => array, 'subject' => string, 'html' => string],
        // ... up to 1,000
    ],
]
Returns
php
BatchResponse
  ->data->batchId  // string
  ->data->queued   // int
  ->data->failed   // int
Example
php
$batch = $client->emails->batch([
    'from' => ['email' => 'hello@yourdomain.com'],
    'messages' => [
        ['to' => [['email' => 'user1@example.com']], 'subject' => 'Hello 1', 'html' => '<p>Hi 1</p>'],
        ['to' => [['email' => 'user2@example.com']], 'subject' => 'Hello 2', 'html' => '<p>Hi 2</p>'],
    ],
]);

echo 'Queued: ' . $batch->data->queued;
GET$client->emails->list()

List sent emails with filtering and cursor-based pagination.

Parameters
php
[
    'cursor' => string|null,
    'limit'  => int,        // default 20, max 100
    'status' => string|null, // 'queued','sent','delivered','bounced','failed'
]
Returns
php
ListResponse
  ->data      // array of email summaries
  ->meta->total
  ->meta->cursor
  ->meta->hasMore
Example
php
$emails = $client->emails->list(['limit' => 50, 'status' => 'delivered']);

foreach ($emails->data as $email) {
    echo $email->id . ' ' . $email->subject . PHP_EOL;
}

if ($emails->meta->hasMore) {
    $next = $client->emails->list(['cursor' => $emails->meta->cursor]);
}
GET$client->emails->get()

Retrieve details of a specific email.

Parameters
php
string $emailId
Returns
php
EmailResponse
  ->data->id, ->data->status, ->data->events
Example
php
$email = $client->emails->get('em_abc123def456');

echo 'Status: ' . $email->data->status;
foreach ($email->data->events as $event) {
    echo $event->type . ' at ' . $event->timestamp . PHP_EOL;
}

GitHub Repository

View source, report issues, and contribute.

View Repository

Other SDKs

Also available for Node.js and Python.