# Send emails with PHP & Laravel

> Call the MailBlastr REST API from Laravel with the Http facade, or from plain PHP with cURL.

MailBlastr does not yet publish a PHP package, but Laravel ships with a clean HTTP client (the `Http` facade, built on Guzzle) that makes calling the REST API a few lines. Plain PHP works too with the built-in cURL extension. Both call `POST /emails` with your `mb_` Bearer token.

This guide shows both the Laravel and the plain-PHP path. Node.js projects can use `npm install mailblastr` instead — see [Send emails with Node.js](https://www.mailblastr.com/docs/integrations/nodejs).

## Prerequisites

1. A **verified domain** for your `from` address. ([guide](https://www.mailblastr.com/docs/domains/managing))
2. An **API key** (`mb_...`), stored in your `.env` as `MAILBLASTR_API_KEY`. ([Authentication](https://www.mailblastr.com/docs/authentication))
3. Laravel 9+ (for the `Http` facade) — or any PHP 8+ with the cURL extension enabled.

## Laravel (Http facade)

Reference the key from `config`/`env`, not inline. The `Http` facade lets you inspect the status and pull JSON fields directly.

**app/Http/Controllers/SendController.php**

```php
<?php

use Illuminate\Support\Facades\Http;

$response = Http::withToken(env('MAILBLASTR_API_KEY'))
    ->acceptJson()
    ->post('https://www.mailblastr.com/api/emails', [
        'from' => 'Acme <hello@yourdomain.com>',
        'to' => ['delivered@example.com'],
        'subject' => 'Hello from Laravel',
        'html' => '<p>Sent with the Http facade.</p>',
    ]);

if ($response->failed()) {
    // MailBlastr returns { statusCode, name, message }.
    abort($response->status(), $response->json('message'));
}

$id = $response->json('id');
```

## Plain PHP (cURL)

Without a framework, the cURL extension sends the same request.

**send.php**

```php
<?php

$apiKey = getenv('MAILBLASTR_API_KEY');

$ch = curl_init('https://www.mailblastr.com/api/emails');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'from' => 'Acme <hello@yourdomain.com>',
        'to' => ['delivered@example.com'],
        'subject' => 'Hello from PHP',
        'html' => '<p>Sent with cURL.</p>',
    ]),
]);

$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$data = json_decode($body, true);
if ($status >= 400) {
    throw new RuntimeException("MailBlastr {$data['name']}: {$data['message']}");
}

echo "Sent email {$data['id']}\n";
```

## Handling the response

A successful send returns HTTP `200` with `{ "id": "..." }`. Errors return a non-2xx status with a `{ statusCode, name, message }` body — check the status (`$response->failed()` or the cURL `HTTP_CODE`) before reading the `id`. Keep the `id` to [retrieve the email](https://www.mailblastr.com/docs/api/emails-get) or correlate [webhook](https://www.mailblastr.com/docs/webhooks/overview) events.

> **Warning:** Store the `mb_` API key in your server's `.env` (or secrets manager) and read it via `env()`/`getenv()`. Never hard-code it, commit it, or output it to a Blade view or browser — the key sends mail as you.

## Next steps

- See the full [Send Email API](https://www.mailblastr.com/docs/api/emails-send) reference.
- Read [Authentication](https://www.mailblastr.com/docs/authentication) for key permissions.
- Start from the [Quickstart](https://www.mailblastr.com/docs/quickstart).
