PHP code example of sendaramail / sendara
1. Go to this page and download the library: Download sendaramail/sendara 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/ */
sendaramail / sendara example snippets
endara\Client;
$client = new Client('sk_live_your_api_key');
$message = $client->emails()->send([
'from' => '[email protected] ',
'to' => '[email protected] ',
'subject' => 'Welcome to Acme',
'html' => '<h1>Welcome aboard</h1><p>Glad to have you.</p>',
'text' => 'Welcome aboard. Glad to have you.',
]);
echo $message['id']; // e.g. "msg_..."
$client = new Client('sk_live_your_api_key', [
'baseUrl' => 'https://api.sendara.dev', // default
'timeout' => 30, // seconds, default 30
'maxRetries' => 2, // default 2
]);
$client->emails()->sendRaw([
'channel' => 'email',
'destination' => ['email' => '[email protected] '],
'payload' => ['subject' => 'Hi', 'body_html' => '<p>Hi</p>'],
]);
$broadcast = $client->broadcasts()->create([
'name' => 'June newsletter',
'fromEmail' => '[email protected] ',
'subject' => 'What shipped in June',
'bodyHtml' => '<h1>June</h1><p>Here is what is new.</p>',
'audienceListId' => 'list_123',
]);
$client->broadcasts()->send($broadcast['id']);
$broadcasts = $client->broadcasts()->list(['limit' => 20, 'offset' => 0]);
$one = $client->broadcasts()->get('bcast_123');
$client->broadcasts()->cancel('bcast_123');
$client->broadcasts()->delete('bcast_123');
$client->broadcasts()->bulkSend([
'fromEmail' => '[email protected] ',
'subject' => 'Launch day',
'bodyHtml' => '<p>We are live.</p>',
'recipients' => [
['email' => '[email protected] '],
['email' => '[email protected] '],
],
]);
$checkout = $client->billing()->checkout('growth', 'year');
if ($checkout->updated) {
echo "Subscription changed to {$checkout->plan}";
} else {
header("Location: {$checkout->url}"); // hosted checkout for a new subscription
}
$page = $client->messages()->page([
'status' => 'delivered',
'limit' => 50,
]);
foreach ($page->messages as $message) {
echo $message['id'], PHP_EOL;
}
if ($page->hasMore()) {
$next = $client->messages()->page(['cursor' => $page->nextCursor]);
}
foreach ($client->messages()->list(['status' => 'delivered']) as $message) {
echo $message['id'], PHP_EOL;
}
$message = $client->messages()->get(['id' => 'msg_123']);
$message = $client->messages()->get(['idempotency_key' => 'order-42']);
use Sendara\Webhooks;
use Sendara\Exception\WebhookVerificationException;
$rawBody = file_get_contents('php://input');
$secret = getenv('SENDARA_WEBHOOK_SECRET');
try {
$event = Webhooks::verify($rawBody, getallheaders(), $secret);
} catch (WebhookVerificationException $e) {
http_response_code(400);
exit('Invalid signature');
}
// $event is the decoded payload
switch ($event['event_type'] ?? null) {
case 'delivered':
// ...
break;
case 'bounced':
// ...
break;
}
http_response_code(200);
$event = Webhooks::verify($rawBody, $headers, $secret, 600);
use Sendara\Exception\ApiException;
use Sendara\Exception\SendaraException;
try {
$client->emails()->send([
'to' => '[email protected] ',
'subject' => 'Hi',
'html' => '<p>Hi</p>',
]);
} catch (ApiException $e) {
$e->getStatus(); // HTTP status, e.g. 422
$e->getErrorCode(); // machine code, e.g. "invalid_request"
$e->getMessage(); // human-readable message
$e->getRequestId(); // X-Request-Id, or null
$e->getRetryAfter(); // seconds from Retry-After, or null
} catch (SendaraException $e) {
// Configuration, encoding, or network errors (no HTTP response)
}
use Sendara\Laravel\SendaraFacade as Sendara;
Sendara::emails()->send([
'from' => '[email protected] ',
'to' => '[email protected] ',
'subject' => 'Welcome',
'html' => '<h1>Welcome</h1>',
]);
$page = Sendara::messages()->page(['limit' => 25]);
use Sendara\Client;
class WelcomeMailer
{
public function __construct(private readonly Client $sendara)
{
}
public function send(string $to): void
{
$this->sendara->emails()->send([
'to' => $to,
'subject' => 'Welcome',
'html' => '<h1>Welcome</h1>',
]);
}
}
bash
php artisan vendor:publish --tag=sendara-config