|
| 1 | +# FlySend PHP SDK |
| 2 | + |
| 3 | +The official PHP SDK for [FlySend](https://flysend.co) — send transactional emails through the FlySend API. |
| 4 | + |
| 5 | +Works with any PHP project. No framework dependencies. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- PHP 8.2+ |
| 10 | +- cURL extension |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +composer require flysend/flysend-php |
| 16 | +``` |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +```php |
| 21 | +use FlySend\FlySend; |
| 22 | + |
| 23 | +$flysend = new FlySend('your-api-key'); |
| 24 | + |
| 25 | +$response = $flysend->emails->send([ |
| 26 | + 'from' => 'hello@example.com', |
| 27 | + 'to' => 'user@example.com', |
| 28 | + 'subject' => 'Welcome!', |
| 29 | + 'html' => '<p>Hello world!</p>', |
| 30 | +]); |
| 31 | + |
| 32 | +echo $response['data']['id']; // email ID |
| 33 | +``` |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +```php |
| 38 | +$flysend = new FlySend( |
| 39 | + apiKey: 'your-api-key', |
| 40 | + baseUrl: 'https://api.flysend.co', // optional, for self-hosted instances |
| 41 | + timeout: 30, // optional, default 30s |
| 42 | +); |
| 43 | +``` |
| 44 | + |
| 45 | +## Send Email |
| 46 | + |
| 47 | +```php |
| 48 | +$response = $flysend->emails->send([ |
| 49 | + 'from' => 'Company <hello@example.com>', |
| 50 | + 'to' => 'user@example.com', |
| 51 | + 'subject' => 'Welcome!', |
| 52 | + 'html' => '<p>Hello!</p>', |
| 53 | + 'text' => 'Hello!', // optional |
| 54 | + 'cc' => 'cc@example.com', // optional |
| 55 | + 'bcc' => 'bcc@example.com', // optional |
| 56 | + 'reply_to' => 'support@example.com', // optional |
| 57 | + 'tags' => [ // optional |
| 58 | + ['name' => 'campaign', 'value' => 'welcome'], |
| 59 | + ], |
| 60 | + 'attachments' => [ // optional |
| 61 | + [ |
| 62 | + 'filename' => 'invoice.pdf', |
| 63 | + 'content' => base64_encode($pdfContent), |
| 64 | + 'mime_type' => 'application/pdf', |
| 65 | + ], |
| 66 | + ], |
| 67 | +]); |
| 68 | + |
| 69 | +echo $response['data']['id']; // email ID |
| 70 | +``` |
| 71 | + |
| 72 | +## Batch Send |
| 73 | + |
| 74 | +Send up to 100 emails in a single request: |
| 75 | + |
| 76 | +```php |
| 77 | +$response = $flysend->emails->batch([ |
| 78 | + [ |
| 79 | + 'from' => 'hello@example.com', |
| 80 | + 'to' => 'user1@example.com', |
| 81 | + 'subject' => 'Hello User 1', |
| 82 | + 'html' => '<p>Hi!</p>', |
| 83 | + ], |
| 84 | + [ |
| 85 | + 'from' => 'hello@example.com', |
| 86 | + 'to' => 'user2@example.com', |
| 87 | + 'subject' => 'Hello User 2', |
| 88 | + 'html' => '<p>Hi!</p>', |
| 89 | + ], |
| 90 | +]); |
| 91 | + |
| 92 | +echo $response['queued_count']; // number of emails queued |
| 93 | +echo $response['error_count']; // number of failures |
| 94 | +``` |
| 95 | + |
| 96 | +## Error Handling |
| 97 | + |
| 98 | +```php |
| 99 | +use FlySend\Exceptions\FlySendException; |
| 100 | +use FlySend\Exceptions\QuotaExceededException; |
| 101 | +use FlySend\Exceptions\ValidationException; |
| 102 | + |
| 103 | +try { |
| 104 | + $flysend->emails->send([...]); |
| 105 | +} catch (QuotaExceededException $e) { |
| 106 | + echo 'Quota exceeded. Remaining: ' . $e->quota['remaining']; |
| 107 | + echo 'Resets at: ' . $e->quota['resets_at']; |
| 108 | +} catch (ValidationException $e) { |
| 109 | + echo 'Validation errors: '; |
| 110 | + print_r($e->errors); // ['field' => ['error message', ...]] |
| 111 | +} catch (FlySendException $e) { |
| 112 | + echo 'API error: ' . $e->getMessage(); |
| 113 | + echo 'Status code: ' . $e->getStatusCode(); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +MIT |
0 commit comments