PHP code example of zemailme / zemail-php

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

    

zemailme / zemail-php example snippets


use Zemail\Client;

$client = new Client('zm_live_your_api_key_here');

$client = new Client(
    apiKey: 'zm_live_your_api_key_here',
    version: '2026-04-23',
    guzzleOptions: [
        'timeout' => 10.0,
        'headers' => [
            'X-Custom-Header' => 'value',
        ],
    ]
);

// Get account profile
$account = $client->account()->get();
echo "Account ID: {$account->id}, Email: {$account->email}, Tier: {$account->tier}\n";

// Get active subscription
$subscription = $client->account()->subscription();
echo "Status: {$subscription->status}, Tier: {$subscription->tier}\n";

// Get current resource & API usage
$usage = $client->account()->usage();
print_r($usage->mailboxes);
print_r($usage->storage);
print_r($usage->developerApi);

$domains = $client->domains()->list();

foreach ($domains->data as $domain) {
    echo "Domain: {$domain->name} (Types: " . implode(', ', $domain->allowedTypes) . ")\n";
}

$mailboxes = $client->mailboxes()->list(page: 1, limit: 10);

foreach ($mailboxes->data as $mailbox) {
    echo "Mailbox: {$mailbox->address} (ID: {$mailbox->id})\n";
}

if ($mailboxes->hasMore) {
    echo "Next cursor: {$mailboxes->nextCursor}\n";
}

$mailbox = $client->mailboxes()->create([
    'type' => 'random',
]);

echo "Created random mailbox: {$mailbox->address}\n";

$mailbox = $client->mailboxes()->create([
    'type' => 'custom',
    'domain' => 'zemail.me',
    'username' => 'my-inbox',
]);

echo "Created custom mailbox: {$mailbox->address}\n";

$mailbox = $client->mailboxes()->get(123);
echo "Address: {$mailbox->address}, Unread emails: {$mailbox->unreadCount}\n";

$deleted = $client->mailboxes()->delete(123);
// Returns true on success

// List recent emails with optional search query
$emails = $client->mailboxes()->emails()->list(
    mailboxId: $mailbox->id,
    page: 1,
    limit: 25,
    search: 'verification'
);

foreach ($emails->data as $email) {
    echo "[{$email->id}] From: {$email->sender} | Subject: {$email->subject}\n";
}

$email = $client->mailboxes()->emails()->get($mailbox->id, $emailId);

echo "Subject: {$email->subject}\n";
echo "Plain text body: {$email->bodyText}\n";
echo "HTML body: {$email->bodyHtml}\n";

// Inspect attachments
foreach ($email->attachments as $attachment) {
    echo "Attachment: {$attachment->name} ({$attachment->size} bytes)\n";
}

$isRead = $client->mailboxes()->emails()->markAsRead($mailbox->id, $emailId);

$download = $client->mailboxes()->emails()->getAttachmentDownloadUrl(
    mailboxId: $mailbox->id,
    emailId: $emailId,
    attachmentId: 'att_123'
);

echo "Download URL: {$download['url']}\n";
echo "Expires at: {$download['expires_at']}\n";

$deleted = $client->mailboxes()->emails()->delete($mailbox->id, $emailId);
// Returns true on success

use Zemail\Exceptions\AuthenticationException;
use Zemail\Exceptions\NotFoundException;
use Zemail\Exceptions\RateLimitException;
use Zemail\Exceptions\ValidationException;
use Zemail\Exceptions\ZemailException;

try {
    $mailbox = $client->mailboxes()->create(['type' => 'custom']);
} catch (ValidationException $e) {
    echo "Validation failed: " . $e->getMessage() . "\n";
    print_r($e->errors);
} catch (AuthenticationException $e) {
    echo "Auth error: " . $e->getMessage() . "\n";
} catch (RateLimitException $e) {
    echo "Rate limited: " . $e->getMessage() . "\n";
} catch (NotFoundException $e) {
    echo "Not found: " . $e->getMessage() . "\n";
} catch (ZemailException $e) {
    echo "General error: " . $e->getMessage() . "\n";
}
bash
composer