PHP code example of maileroo / maileroo-php-sdk

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

    

maileroo / maileroo-php-sdk example snippets




aileroo\MailerooClient;
use Maileroo\EmailAddress;
use Maileroo\Attachment;

// Initialize the client

$client = new MailerooClient('your-api-key');

// Send a basic email

$reference_id = $client->sendBasicEmail([
    'from' => new EmailAddress('[email protected]', 'Sender Name'),
    'to' => [new EmailAddress('[email protected]', 'Recipient Name')],
    'subject' => 'Hello from Maileroo!',
    'html' => '<h1>Hello World!</h1><p>This is a test email.</p>',
    'plain' => 'Hello World! This is a test email.'
]);

echo "Email sent with reference ID: " . $reference_id;



aileroo\MailerooClient;
use Maileroo\EmailAddress;
use Maileroo\Attachment;

$client = new MailerooClient('your-api-key');

$reference_id = $client->sendBasicEmail([
    'from' => new EmailAddress('[email protected]', 'Your Company'),
    'to' => [
        new EmailAddress('[email protected]', 'John Doe'),
        new EmailAddress('[email protected]')
    ],
    'cc' => [new EmailAddress('[email protected]', 'Manager')],
    'bcc' => [new EmailAddress('[email protected]')],
    'reply_to' => new EmailAddress('[email protected]', 'Support Team'),
    'subject' => 'Monthly Report',
    'html' => '<h1>Monthly Report</h1><p>Please find the report attached.</p>',
    'plain' => 'Monthly Report - Please find the report attached.',
    'attachments' => [
        Attachment::fromFile('/path/to/report.pdf', 'application/pdf'),
        Attachment::fromContent('data.csv', $csv_data, 'text/csv')
    ],
    'tracking' => true,
    'tags' => ['campaign' => 'monthly-report', 'type' => 'business'],
    'headers' => [
        'X-Custom-Header' => 'Custom Value',
        'X-Another-Header' => 'Another Value'
    ],
    'reference_id' => $client->getReferenceId()
]);



aileroo\MailerooClient;
use Maileroo\EmailAddress;

$client = new MailerooClient('your-api-key');

$reference_id = $client->sendTemplatedEmail([
    'from' => new EmailAddress('[email protected]', 'Your App'),
    'to' => new EmailAddress('[email protected]', 'John Doe'),
    'subject' => 'Welcome to Our Service!',
    'template_id' => 123,
    'template_data' => [
        'user_name' => 'John Doe',
        'activation_link' => 'https://example.com/activate/abc123',
        'company_name' => 'Your Company'
    ],
    // If "tracking" field is not present, it respects your account settings
]);



aileroo\MailerooClient;
use Maileroo\EmailAddress;

$client = new MailerooClient('your-api-key');

$result = $client->sendBulkEmails([
    'subject' => 'Newsletter - March 2024',
    'html' => '<h1>Hello {{name}}!</h1><p>Here\'s your personalized newsletter content.</p>',
    'plain' => 'Hello {{name}}! Here\'s your personalized newsletter content.',
    'tracking' => false,
    'tags' => ['campaign' => 'newsletter', 'month' => 'march'],
    'messages' => [
        [
            'from' => new EmailAddress('[email protected]', 'Newsletter Team'),
            'to' => new EmailAddress('[email protected]', 'John Doe'),
            'cc' => [new EmailAddress('[email protected]', 'Manager')], // Optional
            'bcc' => [new EmailAddress('[email protected]')], // Optional
            'reply_to' => new EmailAddress('[email protected]', 'Support'), // Optional
            'template_data' => ['name' => 'John'],
            'reference_id' => 'custom-ref-001' // Optional, auto-generated if not provided
        ],
        [
            'from' => new EmailAddress('[email protected]', 'Newsletter Team'),
            'to' => new EmailAddress('[email protected]', 'Jane Smith'),
            'template_data' => ['name' => 'Jane'],
        ]
        // ... up to 500 messages
    ]
]);

foreach ($result as $reference_id) {
    echo "Email sent with reference ID: " . $reference_id . "\n";
}



aileroo\MailerooClient;
use Maileroo\EmailAddress;

$client = new MailerooClient('your-api-key');

$result = $client->sendBulkEmails([
    'subject' => 'Welcome to Our Service!',
    'template_id' => 123, // Use template instead of html/plain
    'tracking' => true,
    'tags' => ['campaign' => 'welcome', 'type' => 'onboarding'],
    'messages' => [
        [
            'from' => new EmailAddress('[email protected]', 'Welcome Team'),
            'to' => new EmailAddress('[email protected]', 'New User 1'),
            'template_data' => [
                'user_name' => 'New User 1',
                'activation_link' => 'https://example.com/activate/token1',
                'company_name' => 'Your Company'
            ]
        ],
        [
            'from' => new EmailAddress('[email protected]', 'Welcome Team'),
            'to' => new EmailAddress('[email protected]', 'New User 2'),
            'template_data' => [
                'user_name' => 'New User 2',
                'activation_link' => 'https://example.com/activate/token2',
                'company_name' => 'Your Company'
            ]
        ]
        // ... up to 500 messages
    ],
    'attachments' => [
        Attachment::fromFile('/path/to/welcome-guide.pdf', 'application/pdf')
    ]
]);

foreach ($result as $reference_id) {
    echo "Email sent with reference ID: " . $reference_id . "\n";
}



aileroo\Attachment;

// From file path:

// Attachment::fromFile(string $path, ?string $content_type = null, bool $inline = false)
$attachment1 = Attachment::fromFile('/path/to/document.pdf', 'application/pdf', false);

// From string content:

// Attachment::fromContent(string $file_name, string $content, ?string $content_type = null, bool $inline = false, bool $is_base64 = false)
$csv_content = "Name,Email\nJohn,[email protected]";
$attachment2 = Attachment::fromContent('contacts.csv', $csv_content, 'text/csv', false, false);

// From base64 content:

// Attachment::fromContent(string $file_name, string $content, ?string $content_type = null, bool $inline = false, bool $is_base64 = true)
$attachment3 = Attachment::fromContent('image.png', $base64_data, 'image/png', false, true);

// From stream resource:

// Attachment::fromStream(string $file_name, resource $stream, ?string $content_type = null, bool $inline = false)
$stream = fopen('/path/to/file.txt', 'r');
$attachment4 = Attachment::fromStream('file.txt', $stream, 'text/plain', false);

// Inline attachment (for embedding in HTML):

// Attachment::fromFile(string $path, ?string $content_type = null, bool $inline = true)
$inline_image = Attachment::fromFile('/path/to/logo.png', 'image/png', true);



aileroo\MailerooClient;
use Maileroo\EmailAddress;

$client = new MailerooClient('your-api-key');

// Schedule email for delivery tomorrow at 9:00 AM UTC

$scheduled_time = date('c', strtotime('+1 day 9:00')); // RFC 3339 format

$reference_id = $client->sendBasicEmail([
    'from' => new EmailAddress('[email protected]', 'Scheduler'),
    'to' => new EmailAddress('[email protected]', 'Recipient'),
    'subject' => 'Scheduled Email - Daily Report',
    'html' => '<h1>Daily Report</h1><p>This email was scheduled for delivery.</p>',
    'plain' => 'Daily Report - This email was scheduled for delivery.',
    'scheduled_at' => $scheduled_time // Schedule for future delivery
]);

echo "Email scheduled with reference ID: " . $reference_id;



aileroo\MailerooClient;

$client = new MailerooClient('your-api-key');

$response = $client->getScheduledEmails($page = 1, $per_page = 20);

// Access pagination info

echo "Page: " . $response['page'] . "/" . $response['total_pages'] . "\n";
echo "Total emails: " . $response['total_count'] . "\n";

// Loop through the results

foreach ($response['results'] as $email) {

    echo "Email ID: " . $email['reference_id'] . "\n";
    echo "From: " . $email['from'] . "\n";
    echo "Subject: " . $email['subject'] . "\n";
    echo "Scheduled for: " . $email['scheduled_at'] . "\n";
    echo "Recipients: " . implode(', ', $email['recipients']) . "\n";
    
    // Show tags if present
    
    if (!empty($email['tags'])) {
        echo "Tags: " . json_encode($email['tags']) . "\n";
    }
    
    // Show custom headers if present
    
    if (!empty($email['headers'])) {
        echo "Headers: " . json_encode($email['headers']) . "\n";
    }
    
    // Cancel a scheduled email if needed
    
    if ($email['reference_id'] === 'some-reference-id') {
        $client->deleteScheduledEmail($email['reference_id']);
        echo "Email cancelled\n";
    }
    
    echo "---\n";
    
}



aileroo\MailerooClient;

$client = new MailerooClient('your-api-key');

try {
    $client->deleteScheduledEmail('your-reference-id');
    echo "Scheduled email cancelled successfully.";
} catch (\Exception $e) {
    echo "Error cancelling scheduled email: " . $e->getMessage();
}


new MailerooClient(string $api_key, int $timeout = 30)

new EmailAddress(string $address, ?string $display_name = null)



use Maileroo\MailerooClient;

try {

    $client = new MailerooClient('your-api-key');
    $reference_id = $client->sendBasicEmail($email_data);
    
    echo "Email sent: " . $reference_id;
    
} catch (\Exception $e) {

    echo "Unexpected error: " . $e->getMessage();
    
}
bash
composer