PHP code example of mailkube / mailkube-php

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

    

mailkube / mailkube-php example snippets


use Mailkube\Client;

$client = new Client();  // reads MAILKUBE_API_KEY

$email = $client->emails->send(
    from: 'Acme <[email protected]>',
    to: '[email protected]',
    subject: 'Hello world',
    html: '<p>It works!</p>',
);

echo $email->id, $email->messageId;

$client = new Client(httpClient: $myPsr18Client, requestFactory: $f, streamFactory: $f);

use Mailkube\ErrorName;
use Mailkube\Exception\RateLimitException;

try {
    $client->emails->send(...);
} catch (RateLimitException $e) {
    // $e->errorName === ErrorName::RateLimitExceeded->value
    sleep($e->retryAfter ?? 1);
}

use Mailkube\Model\Tag;

$client->emails->send(
    from: 'Acme <[email protected]>',
    to: '[email protected]',
    subject: 'Welcome',
    html: '<p>Hi!</p>',
    tags: [new Tag('campaign', 'launch'), new Tag('cohort', 'beta')],
);

$email = $client->emails->send(
    from: 'Acme <[email protected]>',
    to: '[email protected]',
    subject: 'Your trial ends tomorrow',
    html: '<p>Renew?</p>',
    scheduledAt: new DateTimeImmutable('2026-08-20T07:00:00Z'),
    batchId: 'trial-reminders',   // optional label, only valid alongside scheduledAt
);

$page = $client->scheduledEmails->list(status: 'scheduled', batchId: 'trial-reminders');
$email = $client->scheduledEmails->get($id);
$client->scheduledEmails->update($id, scheduledAt: '2026-08-21T07:00:00Z');
$client->scheduledEmails->cancel($id);

foreach ($client->scheduledEmails->iterAll(status: ['scheduled', 'failed']) as $email) {
    echo $email->id, ' ', $email->scheduledAt, ' ', $email->recipients, PHP_EOL;
}

$client->scheduledEmails->batches->update('trial-reminders', '2026-08-22T07:00:00Z');
$result = $client->scheduledEmails->batches->cancel('trial-reminders');
echo $result->canceledCount;   // an unknown batch is a no-op reporting 0, not an error

$key = $client->credential->info();
echo $key->keyId, ' ', $key->domain->name, ' ', $key->organization->alias, PHP_EOL;

foreach ($key->plan?->quotas ?? [] as $quota) {
    echo $quota->name, ' ', $quota->used, '/', $quota->limit, ' ', $quota->period, PHP_EOL;
}

use Mailkube\Event\EmailBouncedEvent;
use Mailkube\Event\EmailClickedEvent;
use Mailkube\Webhooks;

$event = Webhooks::verify($request->getBody()->getContents(), $headers, $signingSecret);

if ($event instanceof EmailBouncedEvent) {
    suppress($event->data->bounce->recipient, $event->data->bounce->reason);
}

if ($event instanceof EmailClickedEvent) {
    track($event->data->emailId, $event->data->click->link);
}

$timestamp = gmdate('c');
$signature = Webhooks::sign('wh_1', $timestamp, $body, $signingSecret);

use Mailkube\Logging;

$client = new Client(logger: $myPsr3Logger);
$client = new Client(logger: Logging::errorLog('debug'));   // no logging framework to hand
bash
composer 
bash
composer 
bash
MAILKUBE_LOG=debug php send.php