PHP code example of misarai / misarmail-php

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

    

misarai / misarmail-php example snippets


use MisarMail\Client;

$mail = new Client(getenv('MISARMAIL_API_KEY'));


MisarMail\Client;

$mail = new Client(getenv('MISARMAIL_API_KEY'));

$sent = $mail->email->send([
    'from'    => ['email' => '[email protected]', 'name' => 'Your App'],
    'to'      => [['email' => '[email protected]']],
    'subject' => 'Hello',
    'html'    => '<p>Hi there</p>',
]);

echo $sent['message_id'];

$res = $mail->email->send([
    'from'            => ['email' => '[email protected]', 'name' => 'Acme'],
    'to'              => [['email' => '[email protected]']],
    'reply_to'        => ['email' => '[email protected]'],
    'subject'         => 'Your receipt',
    'html'            => '<p>Thanks for your order.</p>',
    'text'            => 'Thanks for your order.',
    'tags'            => ['receipt'],
    'metadata'        => ['order_id' => 'ord-1041'],
    'idempotency_key' => 'ord-1041-receipt',
]);

$res['message_id'];   // "msg-…"

$page = $mail->contacts->list(page: 1, limit: 50);
echo count($page['data']), ' of ', $page['pagination']['total'];

$created = $mail->contacts->create([
    'email'        => '[email protected]',
    'firstName'    => 'Ada',
    'lastName'     => 'Lovelace',
    'tags'         => ['beta'],
    'customFields' => ['plan' => 'pro'],
]);

echo $created['data']['id'];

$mail->contacts->update('[email protected]', ['status' => 'unsubscribed']);

$imported = $mail->contacts->import([
    'contacts' => [
        ['email' => '[email protected]', 'firstName' => 'A'],
        ['email' => '[email protected]', 'firstName' => 'B'],
    ],
    'updateExisting' => true,
]);

$imported['summary'];   // ['imported' => …, 'updated' => …, 'skipped' => …, 'errors' => …]
$imported['errors'];    // list of strings

$campaign = $mail->campaigns->create([
    'name'      => 'March launch',
    'subject'   => 'We just shipped',
    'fromName'  => 'Ada at Acme',
    'fromEmail' => '[email protected]',
    'replyTo'   => '[email protected]',
    'bodyHtml'  => '<h1>It is live</h1>',
    'segmentId' => 'seg-123',
]);

$queued = $mail->campaigns->send($campaign['data']['id']);
echo $queued['campaignId'], ' ', $queued['status'];   // … scheduled

$check = $mail->validate->email('[email protected]');

$check['data']['is_valid'];             // bool
$check['data']['score'];                // 0–1 confidence
$check['data']['checks'];               // ['syntax' => …, 'mx' => …, 'smtp' => …]
$check['data']['flags']['disposable'];  // bool
$check['credits']['balance_after'];     // credits remaining

$rendered = $mail->templates->render([
    'template_id' => 'tpl-123',
    'variables'   => ['name' => 'Ada', 'plan' => 'Pro'],
]);

$rendered['data']['subject'];   // "Welcome, Ada"
$rendered['data']['html'];

$mail->track->event([
    'email'      => '[email protected]',
    'event_name' => 'viewed_pricing',
    'event_data' => ['plan' => 'pro'],
]);

$purchase = $mail->track->purchase([
    'email'       => '[email protected]',
    'order_id'    => 'ord-1041',
    'total_cents' => 9900,
    'currency'    => 'USD',
    'items'       => [['name' => 'Pro annual', 'quantity' => 1, 'price_cents' => 9900]],
]);

$purchase['attribution'];   // which campaign or automation earned it

$overall = $mail->analytics->overview(['startDate' => '2026-04-01', 'endDate' => '2026-04-30']);
$one     = $mail->analytics->overview(['campaignId' => $campaignId]);

$keys = $mail->keys->list();
count($keys['keys']);

$fresh = $mail->keys->create(['name' => 'CI', 'scopes' => ['send', 'read']]);
echo $fresh['key'];   // shown once and never again

try {
    $mail->campaigns->create([
        'name'      => 'Blast',
        'subject'   => 'We just shipped',
        'fromName'  => 'Your Name',
        'fromEmail' => '[email protected]',
    ]);
} catch (MisarMail\PlanLimitError $e) {
    fwrite(STDERR, "{$e->feature} exhausted on {$e->plan}: {$e->upgradeUrl}\n");
    // $e->retryAfter is seconds until the allowance resets, when the API says so
}

$plan = $mail->plan->get();
print_r($plan['sending']);
print_r($plan['usage']);

$mail->streaming->generateEmail(['prompt' => 'a launch email'],
    function (MisarMail\StreamEvent $e) {
        echo $e->data['delta'] ?? '';
    });

$mail->streaming->campaignSend($campaignId, function (MisarMail\StreamEvent $e) {
    echo $e->raw, "\n";
});
bash
composer