Skip to content

weMail/flysend-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlySend PHP SDK

The official PHP SDK for FlySend — send transactional emails through the FlySend API.

Works with any PHP project. No framework dependencies.

Requirements

  • PHP 8.2+
  • cURL extension

Installation

composer require flysend/flysend-php

Quick Start

use FlySend\FlySend;

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

$response = $flysend->emails->send([
    'from' => 'hello@example.com',
    'to' => 'user@example.com',
    'subject' => 'Welcome!',
    'html' => '<p>Hello world!</p>',
]);

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

Configuration

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

Send Email

$response = $flysend->emails->send([
    'from'    => 'Company <hello@example.com>',
    'to'      => 'user@example.com',
    'subject' => 'Welcome!',
    'html'    => '<p>Hello!</p>',
    'text'    => 'Hello!',                              // optional
    'cc'      => 'cc@example.com',                      // optional
    'bcc'     => 'bcc@example.com',                     // optional
    'reply_to' => 'support@example.com',                // 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

Batch Send

Send up to 100 emails in a single request:

$response = $flysend->emails->batch([
    [
        'from'    => 'hello@example.com',
        'to'      => 'user1@example.com',
        'subject' => 'Hello User 1',
        'html'    => '<p>Hi!</p>',
    ],
    [
        'from'    => 'hello@example.com',
        'to'      => 'user2@example.com',
        'subject' => 'Hello User 2',
        'html'    => '<p>Hi!</p>',
    ],
]);

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

Error Handling

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();
}

License

MIT

About

FlySend PHP SDK for sending emails via the FlySend API.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages