PHP code example of s-mailer / sdk

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

    

s-mailer / sdk example snippets


$mailer = new \SMailer\Client('CLT-XXXXXXXX', 'your-api-secret');

$result = $mailer->messages->send([
    'sender'    => 'sms-co1-xxxxxxxx',
    'recipients' => [
        '+258841234567',
        ['phone' => '+258842000001', 'data' => ['name' => 'Alice']],
    ],
    'content_template' => 'Hi ${name}, your code is ${code}.',
    // 'channel' => 'sms', 'provider' => 'sms-co1',  // alternatives to `sender`
    // 'campaign_id' => 'promo-2026',
    // 'webhook_url' => 'https://your-app.com/webhooks/status',
]);
// => ['message_id' => ..., 'external_id' => ..., 'total_outbound_cost_tokens' => ...]

$sent = $mailer->messages->listOutbound(['status' => 'delivered', 'channel' => 'sms', 'limit' => 50]);
$received = $mailer->messages->listInbound(['limit' => 50]);
$one = $mailer->messages->get($messageId);   // outbound or inbound; see `direction`

$mailer->messages->retrigger($inboundId); // unlock + re-deliver a locked (unpaid) inbound
$mailer->messages->delete($messageId);    // delete an outbound or paid inbound message

$tokens   = $mailer->balance->get();       // int
$profile  = $mailer->balance->profile();   // )

$sender = 'whatsapp-co1-1a2b3c4d';
$to     = '+258841234567';

$mailer->whatsapp->sendText($sender, $to, 'Hello 👋');

// Host media anywhere publicly reachable, then pass the URL.
$mailer->whatsapp->sendMedia($sender, $to, [
    'kind' => 'image', 'url' => 'https://cdn.example.com/promo.jpg', 'filename' => 'promo.jpg',
], caption: 'Our new catalogue');

$res = $mailer->whatsapp->sendButtons($sender, $to, 'Confirm your appointment?', [
    ['id' => 'yes', 'title' => 'Yes'],
    ['id' => 'no',  'title' => 'No'],
], footer: 'S-Mailer');

// Location pin.
$mailer->whatsapp->sendLocation($sender, $to, -25.9655, 32.5832, title: 'Smartek HQ');

// Contact card (phone is E.164; becomes the card's tap-to-chat number).
$mailer->whatsapp->sendContact($sender, $to, [
    'name' => 'John Doe', 'phone' => '+258841112222', 'organization' => 'Acme Lda',
]);

// React to a message (outbound or inbound); '' removes the reaction.
$mailer->whatsapp->react($sender, $res['message_id'], '👍');

$mailer = new \SMailer\Client('CLT-XXXXXXXX', 'your-api-secret', [
    'baseUrl'    => 'https://api.mailer.smartek.co.mz', // override the API host
    'maxRetries' => 2,                                  // retry 429/5xx/network (default 2)

    // Inject your own PSR-18 client / PSR-17 factories instead of auto-discovery:
    // 'httpClient'     => $psr18Client,
    // 'requestFactory' => $psr17RequestFactory,
    // 'streamFactory'  => $psr17StreamFactory,
]);

$raw = file_get_contents('php://input');
$sig = $_SERVER['HTTP_X_MAILER_SIGNATURE'] ?? '';

$event = (new \SMailer\Webhook())->parse($raw, $sig, $signingKey); // throws on bad signature
if (($event['payment_

use SMailer\Exception\InsufficientTokensException;
use SMailer\Exception\RateLimitException;

try {
    $mailer->messages->send([/* ... */]);
} catch (InsufficientTokensException $e) {
    // top up the account
} catch (RateLimitException $e) {
    sleep($e->retryAfter ?? 1);
}