PHP code example of mailbreeze / mailbreeze-php

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

    

mailbreeze / mailbreeze-php example snippets




use MailBreeze\MailBreeze;

$client = new MailBreeze('your_api_key');

// Send an email
$email = $client->emails->send([
    'from' => '[email protected]',
    'to' => ['[email protected]'],
    'subject' => 'Hello from MailBreeze!',
    'html' => '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
]);

echo "Email sent with ID: " . $email['id'];

// Simple email
$email = $client->emails->send([
    'from' => '[email protected]',
    'to' => ['[email protected]'],
    'subject' => 'Welcome!',
    'html' => '<p>Hello World</p>',
    'text' => 'Hello World',
]);

// With template
$email = $client->emails->send([
    'from' => '[email protected]',
    'to' => ['[email protected]'],
    'template_id' => 'tmpl_welcome',
    'variables' => [
        'name' => 'John',
        'company' => 'Acme Inc',
    ],
]);

// With attachments
$email = $client->emails->send([
    'from' => '[email protected]',
    'to' => ['[email protected]'],
    'subject' => 'Your Invoice',
    'html' => '<p>Please find your invoice attached.</p>',
    'attachment_ids' => ['attach_123'],
]);

// Get email status
$email = $client->emails->get('email_123');

// List emails
$result = $client->emails->list([
    'status' => 'delivered',
    'page' => 1,
    'limit' => 20,
]);

// Get statistics
$stats = $client->emails->stats();

// Create contact
$contact = $client->contacts->create([
    'email' => '[email protected]',
    'first_name' => 'John',
    'last_name' => 'Doe',
    'custom_fields' => [
        'company' => 'Acme Inc',
        'plan' => 'enterprise',
    ],
]);

// Update contact
$contact = $client->contacts->update('contact_123', [
    'first_name' => 'Johnny',
]);

// Get contact
$contact = $client->contacts->get('contact_123');

// List contacts
$result = $client->contacts->list([
    'status' => 'active',
    'search' => 'john',
]);

// Unsubscribe contact
$client->contacts->unsubscribe('contact_123');

// Resubscribe contact
$client->contacts->resubscribe('contact_123');

// Delete contact
$client->contacts->delete('contact_123');

// Create list
$list = $client->lists->create([
    'name' => 'Newsletter Subscribers',
    'description' => 'Weekly newsletter recipients',
]);

// Add contact to list
$client->lists->addContact('list_123', 'contact_456');

// Remove contact from list
$client->lists->removeContact('list_123', 'contact_456');

// Get list contacts
$result = $client->lists->contacts('list_123');

// Get list statistics
$stats = $client->lists->stats('list_123');

// Verify single email
$result = $client->verification->verify(['email' => '[email protected]']);

if ($result['is_valid']) {
    echo "Email is valid!";
} else {
    echo "Email is invalid: " . $result['status'];
}

// Batch verification
$batch = $client->verification->batch([
    '[email protected]',
    '[email protected]',
    '[email protected]',
]);

// Check batch status
$result = $client->verification->get($batch['verification_id']);

// List verification jobs
$verifications = $client->verification->list([
    'page' => 1,
    'limit' => 20,
]);

// Get verification statistics
$stats = $client->verification->stats();

// Create upload URL
$upload = $client->attachments->createUploadUrl([
    'filename' => 'invoice.pdf',
    'contentType' => 'application/pdf',
    'size' => 102400,
]);

// Upload file to the URL
// Use your preferred HTTP client to PUT the file to $upload['uploadUrl']

// Use the attachment ID when sending emails
$email = $client->emails->send([
    'from' => '[email protected]',
    'to' => ['[email protected]'],
    'subject' => 'Your Invoice',
    'html' => '<p>Please find your invoice attached.</p>',
    'attachmentIds' => [$upload['attachmentId']],
]);

$client = new MailBreeze('your_api_key', [
    'base_url' => 'https://api.mailbreeze.com', // Custom API URL (default)
    'timeout' => 30,                            // Request timeout in seconds
    'max_retries' => 3,                         // Maximum retry attempts
    'retry_delay' => 1000,                      // Base retry delay in milliseconds
]);

use MailBreeze\Exceptions\AuthenticationException;
use MailBreeze\Exceptions\BadRequestException;
use MailBreeze\Exceptions\NotFoundException;
use MailBreeze\Exceptions\RateLimitException;
use MailBreeze\Exceptions\ValidationException;
use MailBreeze\Exceptions\ServerException;

try {
    $email = $client->emails->send([...]);
} catch (AuthenticationException $e) {
    // Invalid API key (401)
    echo "Authentication failed: " . $e->getMessage();
} catch (ValidationException $e) {
    // Validation errors (422)
    echo "Validation failed: " . $e->getMessage();
    print_r($e->getErrors()); // Field-specific errors
} catch (RateLimitException $e) {
    // Rate limited (429)
    echo "Rate limited. Retry after: " . $e->getRetryAfter() . " seconds";
} catch (NotFoundException $e) {
    // Resource not found (404)
    echo "Not found: " . $e->getMessage();
} catch (BadRequestException $e) {
    // Bad request (400)
    echo "Bad request: " . $e->getMessage();
} catch (ServerException $e) {
    // Server error (5xx)
    echo "Server error: " . $e->getMessage();
}
bash
composer