PHP code example of sendclipper / sendclipper-php

1. Go to this page and download the library: Download sendclipper/sendclipper-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sendclipper / sendclipper-php example snippets


use SendClipper\SendClipper;

$sc = new SendClipper('sc_live_xxxxxxxxxxxxxxxx');
// or: new SendClipper()  reads SENDCLIPPER_API_KEY

[$data, $error] = $sc->emails->send([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'subject' => 'Your receipt',
    'html' => '<p>Thanks for your order.</p>',
    'text' => 'Thanks for your order.',
]);

if ($error) {
    echo $error->name, ': ', $error->message; // e.g. domain_not_verified
} else {
    echo "Email {$data['id']} accepted";
}

[$data, $error] = $sc->emails->send([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'subject' => 'Your booking',
    'html' => '<p>Your confirmation is attached.</p>',
    'replyTo' => '[email protected]',
    'attachments' => [
        ['filename' => 'booking-42.pdf', 'content' => file_get_contents('booking-42.pdf')],
    ],
]);

$sc->emails->send([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'templateId' => 'tpl_9f2b7c10ad42', // or 'template' => 'welcome'
    'variables' => ['first_name' => 'Ada'],
]);

[$data] = $sc->batch->send([
    ['from' => '[email protected]', 'to' => '[email protected]', 'subject' => 'Hi Ada', 'html' => '<p>Hi</p>'],
    ['from' => '[email protected]', 'to' => '[email protected]', 'templateId' => 'tpl_9f2b7c10ad42', 'variables' => ['first_name' => 'Alan']],
]);

foreach ($data['data'] as $item) {
    if ($item['status'] === 'failed') {
        echo $item['index'], ' ', $item['error']['code'], PHP_EOL;
    }
}

$result = $sc->domains->list();
$result->data;     // the decoded response, or null
$result->error;    // a SendClipper\ErrorResponse, or null
$result->headers;  // response headers (lower-case names), or null when no answer arrived
$result->ok();     // error === null

$domains = $sc->domains->list()->unwrap(); // data, or throws SendClipper\Exception\ApiException

$sc->emails->send($params, ['idempotencyKey' => "order-{$order->id}-receipt"]);

$sc->emails->list(['status' => 'bounced'], ['timeout' => 5, 'headers' => ['X-Trace-Id' => $traceId]]);

use SendClipper\Exception\WebhookVerificationException;
use SendClipper\Webhook;

try {
    $event = Webhook::verify(
        file_get_contents('php://input'), // the raw body, not json_decode()d
        getallheaders(),
        (string) getenv('SENDCLIPPER_WEBHOOK_SECRET'),
    );
} catch (WebhookVerificationException $e) {
    http_response_code(400);
    exit($e->reason);
}
// $event['id'] is stable across retries: deduplicate on it
error_log($event['event'] . ' ' . $event['data']['to']);
http_response_code(200);

$box = $sc->sandboxes->create(['name' => 'my-app-dev'])->unwrap();
$creds = $sc->sandboxes->credentials($box['id'])->unwrap(); // SMTP host, port, username, password

// Or send through the sandbox's JSON door with its own token, using the same fields as emails->send():
$token = $sc->sandboxes->resetCredentials($box['id'])->unwrap();
$sc->sandboxes->send($token['apiToken'], ['from' => '[email protected]', 'to' => '[email protected]', 'subject' => 'Hi', 'html' => '<p>Hi</p>']);

// In a test: wait for the message to arrive
[$msg, $error] = $sc->sandboxes->messages->waitFor($box['id'], fn (array $m) => $m['subject'] === 'Hi');
bash
composer