PHP code example of woyra / woyra-php

1. Go to this page and download the library: Download woyra/woyra-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/ */

    

woyra / woyra-php example snippets



ra = new Woyra\Client('wk_live_...'); // or new Woyra\Client() to read WOYRA_API_KEY

$email = $woyra->emails->send([
    'from' => 'Acme <[email protected]>',
    'to' => '[email protected]',          // a string, or an array of strings
    'subject' => 'Your code',
    'text' => 'Your code is 483920',
]);
echo $email['id'], ' ', $email['status'];

$woyra = new Woyra\Client('wk_live_...', [
    'base_url' => 'https://kenya.woyra.io', // default https://api.woyra.io (or WOYRA_BASE_URL)
    'max_retries' => 3,                     // retries after the first attempt
    'timeout' => 30.0,                      // per attempt, in seconds
    'api_version' => '2026-09-01',          // sent as Woyra-Version when set
]);

$template = $woyra->templates->create([
    'name' => 'welcome',
    'subject' => 'Welcome, {{name}}',
    'html' => '<p>Hi {{name}}, your plan is {{plan}}.</p>',
]);
print_r($template['variables']); // ['name', 'plan']

$woyra->emails->send([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'template_id' => $template['id'],
    'variables' => ['name' => 'Abebe', 'plan' => 'Pro'],
    'track_opens' => true,
    'track_clicks' => true,
]);

$result = $woyra->emails->batch([
    ['from' => '[email protected]', 'to' => '[email protected]', 'subject' => 'Digest', 'text' => '...'],
    ['from' => '[email protected]', 'to' => ['[email protected]', '[email protected]'], 'subject' => 'Digest', 'text' => '...'],
]);

$scheduled = $woyra->emails->send([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'subject' => 'Reminder',
    'text' => 'Your appointment is tomorrow.',
    'scheduled_at' => new DateTimeImmutable('+1 day'), // or an RFC 3339 string
]);

$woyra->emails->cancel($scheduled['id']);

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

$domain = $woyra->domains->create('yourdomain.com');
print_r($domain['records']); // DNS records to publish
$woyra->domains->verify($domain['id']);

$endpoint = $woyra->webhooks->create('https://api.yourapp.com/hooks/woyra', ['email.delivered', 'email.opened', 'email.clicked']);
echo $endpoint['secret']; // shown once: store it

use Woyra\Webhook;
use Woyra\WebhookVerificationException;

$payload = file_get_contents('php://input');
try {
    $event = Webhook::verify($payload, getallheaders(), getenv('WOYRA_WEBHOOK_SECRET'));
} catch (WebhookVerificationException $e) {
    http_response_code(400);
    exit;
}
if ($event['type'] === 'email.delivered') {
    // $event['data']['email_id'] ...
}
http_response_code(204);

use Woyra\WoyraException;

try {
    $woyra->emails->send(['from' => '[email protected]', 'to' => 'not-an-address', 'subject' => 'x', 'text' => 'x']);
} catch (WoyraException $e) {
    echo $e->status;        // 400
    echo $e->errorCode;     // 'invalid_request'  (branch on this)
    echo $e->getMessage();  // '"not-an-address" is not a valid email address.'
    echo $e->param;         // 'to'
    echo $e->requestId;     // 'req_...' (quote it to support)
    echo $e->docUrl;
}
sh
php tests/run.php   # or: composer test