> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailofly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Php

# PHP SDK

Official **PHP** client for the Mailofly REST API.

**Package:** [`mailofly/mailofly`](https://packagist.org/packages/mailofly/mailofly) · **Requires:** PHP 8.1+ · **Quickstart:** [PHP](../getting-started/php.md)

***

## Install

```bash theme={null}
composer require mailofly/mailofly
```

***

## Initialize

```php theme={null}
<?php

use Mailofly\Client;
use Mailofly\MailoflyException;

$client = new Client(
    getenv('MAILOFLY_API_KEY'),
    // 'https://www.mailofly.com', // optional baseUrl
);
```

Create an API key under **[API keys](https://www.mailofly.com/user/api-keys)** first.

***

## Send email

```php theme={null}
try {
    $result = $client->emails->send([
        'from' => 'Acme <onboarding@example.com>',
        'to' => ['alex@example.com'],
        'subject' => 'Hello',
        'html' => '<p>Thanks for signing up.</p>',
    ]);
    echo $result['id'];
} catch (MailoflyException $e) {
    fwrite(STDERR, "{$e->status} {$e->error} {$e->detailMessage}\n");
    throw $e;
}
```

### With a saved template

```php theme={null}
$client->emails->send([
    'from' => 'Acme <onboarding@example.com>',
    'to' => ['alex@example.com'],
    'template' => [
        'id' => 'uuid-of-template',
        'variables' => ['first_name' => 'Alex'],
    ],
]);
```

`$client->compose->send` is deprecated and forwards to `/v1/emails`.

### Batch send (up to 100 emails)

```php theme={null}
$result = $client->batch->send([
    [
        'from' => 'Acme <onboarding@example.com>',
        'to' => ['foo@example.com'],
        'subject' => 'hello world',
        'html' => '<h1>it works!</h1>',
    ],
    [
        'from' => 'Acme <onboarding@example.com>',
        'to' => ['bar@example.com'],
        'subject' => 'world hello',
        'html' => '<p>it works!</p>',
    ],
]);
```

***

## Resources

| Namespace            | Methods                                                                  |
| -------------------- | ------------------------------------------------------------------------ |
| `$client->accounts`  | `list`, `create`, `get`, `update`, `delete`                              |
| `$client->contacts`  | `list`, `create`, `get`, `update`, `delete`                              |
| `$client->templates` | `list`, `create`, `get`, `update`, `delete`                              |
| `$client->segments`  | `list`, `create`, `get`, `update`, `delete`, `contacts->list/add/remove` |
| `$client->campaigns` | `list`, `create`, `get`, `update`, `delete`, `runs`, `send`              |
| `$client->emails`    | `list`, `get`, `send`                                                    |
| `$client->batch`     | `send`                                                                   |
| `$client->compose`   | `send` (deprecated)                                                      |
| `$client->mailLogs`  | `list`                                                                   |

### Examples

```php theme={null}
$accounts = $client->accounts->list();

$client->contacts->create([
    'email' => 'alex@example.com',
    'first_name' => 'Alex',
    'custom_fields' => ['plan' => 'pro'],
]);

$client->campaigns->send('campaign-uuid', ['send_now' => true]);

$logs = $client->mailLogs->list(['page' => 1, 'page_size' => 50, 'status' => 'failed']);
```

***

## Discovery (no API key)

```php theme={null}
$meta = Client::discovery();
print_r($meta);
```

***

## Error handling

```php theme={null}
try {
    $client->accounts->list();
} catch (MailoflyException $e) {
    // $e->status, $e->error, $e->detailMessage
}
```

***

## Related

* [PHP quickstart](../getting-started/php.md)
* [API: Emails](https://docs.mailofly.com/api/emails)


## Related topics

- [SDKs](/sdks/index.md)
- [Php](/getting-started/php.md)
- [Http](/getting-started/http.md)
- [Manage Emails](/sending/manage-emails.md)
- [Introduction](/sending/introduction.md)
