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

# PHP SDK

> Official PHP SDK for MailBreeze

## Installation

```bash theme={null}
composer require mailbreeze/mailbreeze-php
```

## Requirements

* PHP 8.1+
* Guzzle HTTP client (auto-installed)

## Quick Start

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

require_once 'vendor/autoload.php';

use MailBreeze\MailBreeze;

$mailbreeze = new MailBreeze(getenv('MAILBREEZE_API_KEY'));

$result = $mailbreeze->emails->send([
    'from' => 'hello@yourdomain.com',
    'to' => ['user@example.com'],
    'subject' => 'Welcome!',
    'html' => '<h1>Welcome to our platform!</h1>',
]);

echo "Email sent: " . $result['data']['id'];
```

## Configuration

```php theme={null}
$mailbreeze = new MailBreeze(
    getenv('MAILBREEZE_API_KEY'),
    [
        'base_url' => 'https://api.mailbreeze.com',  // Optional
        'timeout' => 30,                              // Optional (seconds)
        'max_retries' => 3,                           // Optional
    ]
);
```

## Resources

### Emails

```php theme={null}
// Send email
$result = $mailbreeze->emails->send([
    'from' => 'hello@yourdomain.com',
    'to' => ['user@example.com'],
    'subject' => 'Hello',
    'html' => '<p>Hello!</p>',
]);
echo "Sent: " . $result['data']['id'];

// Send with template
$result = $mailbreeze->emails->send([
    'from' => 'hello@yourdomain.com',
    'to' => ['user@example.com'],
    'template_id' => 'welcome-template',
    'variables' => ['name' => 'John'],
]);

// List emails
$result = $mailbreeze->emails->list([
    'status' => 'delivered',
    'page' => 1,
    'limit' => 20,
]);
foreach ($result['data'] as $email) {
    echo $email['subject'] . "\n";
}

// Get email
$email = $mailbreeze->emails->get('msg_xxx');

// Get stats
$stats = $mailbreeze->emails->stats();
echo "Total: " . $stats['data']['total'];
```

### Attachments

```php theme={null}
// Create presigned upload URL
$upload = $mailbreeze->attachments->createUploadUrl([
    'filename' => 'report.pdf',
    'contentType' => 'application/pdf',
    'size' => 1024000,
]);

// Upload file (use Guzzle or any HTTP client)
$client = new \GuzzleHttp\Client();
$client->put($upload['data']['uploadUrl'], [
    'body' => file_get_contents('report.pdf'),
    'headers' => ['Content-Type' => 'application/pdf'],
]);

// Use the attachment ID when sending emails
$mailbreeze->emails->send([
    'from' => 'hello@yourdomain.com',
    'to' => ['user@example.com'],
    'subject' => 'Your Report',
    'html' => '<p>Please find your report attached.</p>',
    'attachment_ids' => [$upload['data']['attachmentId']],
]);
```

### Lists

```php theme={null}
// Create list
$list = $mailbreeze->lists->create([
    'name' => 'Newsletter',
    'description' => 'Weekly newsletter subscribers',
]);
echo "Created: " . $list['data']['id'];

// List all
$result = $mailbreeze->lists->list();
foreach ($result['data'] as $list) {
    echo $list['name'] . "\n";
}

// Get, update, delete
$list = $mailbreeze->lists->get('lst_xxx');
$list = $mailbreeze->lists->update('lst_xxx', [
    'name' => 'New Name',
]);
$mailbreeze->lists->delete('lst_xxx');

// Get stats
$stats = $mailbreeze->lists->stats('lst_xxx');

// Add/remove contacts from list
$mailbreeze->lists->addContact('lst_xxx', 'cnt_xxx');
$mailbreeze->lists->removeContact('lst_xxx', 'cnt_xxx');

// Get list contacts
$contacts = $mailbreeze->lists->contacts('lst_xxx');
```

### Contacts

```php theme={null}
// Create contact
$contact = $mailbreeze->contacts->create([
    'email' => 'user@example.com',
    'first_name' => 'John',
    'last_name' => 'Doe',
    'custom_fields' => ['company' => 'Acme'],
]);
echo "Created: " . $contact['data']['id'];

// List contacts
$result = $mailbreeze->contacts->list(['status' => 'active']);
foreach ($result['data'] as $contact) {
    echo $contact['email'] . "\n";
}

// Get, update, delete
$contact = $mailbreeze->contacts->get('cnt_xxx');
$contact = $mailbreeze->contacts->update('cnt_xxx', [
    'first_name' => 'Jane',
]);
$mailbreeze->contacts->delete('cnt_xxx');

// Unsubscribe/resubscribe contact
$mailbreeze->contacts->unsubscribe('cnt_xxx');
$mailbreeze->contacts->resubscribe('cnt_xxx');
```

### Verification

```php theme={null}
// Verify single email
$result = $mailbreeze->verification->verify(['email' => 'user@example.com']);
if ($result['data']['is_valid']) {
    echo "Safe to send";
} else {
    echo "Invalid: " . $result['data']['status'];
}

// Batch verify
$batch = $mailbreeze->verification->batch([
    'user1@example.com',
    'user2@example.com',
]);

// Poll for results
$status = $mailbreeze->verification->get($batch['data']['verification_id']);

// List verifications and get stats
$result = $mailbreeze->verification->list();
$stats = $mailbreeze->verification->stats();
```

## Error Handling

```php theme={null}
use MailBreeze\MailBreeze;
use MailBreeze\Exceptions\MailBreezeException;
use MailBreeze\Exceptions\ValidationException;
use MailBreeze\Exceptions\AuthenticationException;
use MailBreeze\Exceptions\RateLimitException;

try {
    $mailbreeze->emails->send([...]);
} catch (ValidationException $e) {
    // Invalid request data (422)
    echo $e->getMessage();     // Human-readable message
    print_r($e->getErrors());  // Field-specific errors
} catch (AuthenticationException $e) {
    // Invalid API key (401)
    echo $e->getMessage();
} catch (RateLimitException $e) {
    // Too many requests (429)
    echo "Retry after: " . $e->getRetryAfter() . " seconds";
} catch (MailBreezeException $e) {
    // Other API errors
    echo $e->getMessage();
}
```

## Exception Types

| Exception                 | HTTP Status | Description                |
| ------------------------- | ----------- | -------------------------- |
| `ValidationException`     | 400         | Invalid request parameters |
| `AuthenticationException` | 401         | Invalid or missing API key |
| `ForbiddenException`      | 403         | Access denied to resource  |
| `NotFoundException`       | 404         | Resource not found         |
| `RateLimitException`      | 429         | Rate limit exceeded        |
| `ServerException`         | 5xx         | Server-side error          |
| `MailBreezeException`     | \*          | Base exception class       |

## Laravel Integration

```php theme={null}
// config/services.php
return [
    'mailbreeze' => [
        'api_key' => env('MAILBREEZE_API_KEY'),
    ],
];

// app/Providers/AppServiceProvider.php
use MailBreeze\MailBreeze;

public function register(): void
{
    $this->app->singleton(MailBreeze::class, function () {
        return new MailBreeze(config('services.mailbreeze.api_key'));
    });
}

// Usage in controller
public function sendWelcome(Request $request, MailBreeze $mailbreeze)
{
    $mailbreeze->emails->send([
        'from' => 'welcome@yourdomain.com',
        'to' => [$request->user()->email],
        'template_id' => 'welcome',
        'variables' => ['name' => $request->user()->name],
    ]);
}
```

## Features

* **PSR-4 autoloading** - Standard PHP autoloading
* **Guzzle HTTP** - Battle-tested HTTP client
* **Typed exceptions** - Specific exception classes for each error type
* **Automatic retries** - Exponential backoff for transient failures
* **PHPStan level 8** - Full static analysis
* **95%+ test coverage**

## Links

<CardGroup cols={2}>
  <Card title="Packagist" icon="php" href="https://packagist.org/packages/mailbreeze/mailbreeze-php">
    View on Packagist
  </Card>

  <Card title="GitHub" icon="github" href="https://github.com/MailBreeze/mailbreeze-php">
    Source code
  </Card>
</CardGroup>
