PHP code example of flysend / flysend-php

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

    

flysend / flysend-php example snippets


use FlySend\FlySend;

$flysend = new FlySend('your-api-key');

$response = $flysend->emails->send([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'subject' => 'Welcome!',
    'html' => '<p>Hello world!</p>',
]);

echo $response['data']['id']; // email ID

$flysend = new FlySend(
    apiKey: 'your-api-key',
    baseUrl: 'https://api.flysend.co', // optional, for self-hosted instances
    timeout: 30,                        // optional, default 30s
);

$response = $flysend->emails->send([
    'from'    => 'Company <[email protected]>',
    'to'      => '[email protected]',
    'subject' => 'Welcome!',
    'html'    => '<p>Hello!</p>',
    'text'    => 'Hello!',                              // optional
    'cc'      => '[email protected]',                      // optional
    'bcc'     => '[email protected]',                     // optional
    'reply_to' => '[email protected]',                // optional
    'tags'    => [                                      // optional
        ['name' => 'campaign', 'value' => 'welcome'],
    ],
    'attachments' => [                                  // optional
        [
            'filename'  => 'invoice.pdf',
            'content'   => base64_encode($pdfContent),
            'mime_type' => 'application/pdf',
        ],
    ],
]);

echo $response['data']['id']; // email ID

$response = $flysend->emails->batch([
    [
        'from'    => '[email protected]',
        'to'      => '[email protected]',
        'subject' => 'Hello User 1',
        'html'    => '<p>Hi!</p>',
    ],
    [
        'from'    => '[email protected]',
        'to'      => '[email protected]',
        'subject' => 'Hello User 2',
        'html'    => '<p>Hi!</p>',
    ],
]);

echo $response['queued_count']; // number of emails queued
echo $response['error_count'];  // number of failures

use FlySend\Exceptions\FlySendException;
use FlySend\Exceptions\QuotaExceededException;
use FlySend\Exceptions\ValidationException;

try {
    $flysend->emails->send([...]);
} catch (QuotaExceededException $e) {
    echo 'Quota exceeded. Remaining: ' . $e->quota['remaining'];
    echo 'Resets at: ' . $e->quota['resets_at'];
} catch (ValidationException $e) {
    echo 'Validation errors: ';
    print_r($e->errors); // ['field' => ['error message', ...]]
} catch (FlySendException $e) {
    echo 'API error: ' . $e->getMessage();
    echo 'Status code: ' . $e->getStatusCode();
}
bash
composer