# Send emails with PHP & Laravel

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

SendPing ships an official PHP package — `composer require sendping/sendping`. If you want it, start at [Send emails with PHP](https://www.sendping.co/docs/integrations/php), which installs the package and sends a first email with `$sendping->emails->send(...)`.

This guide shows the raw-HTTP alternative instead: Laravel ships a clean HTTP client (the `Http` facade, built on Guzzle) that makes calling the REST API a few lines, and plain PHP works with the built-in cURL extension. Both call `POST /emails` with your `mb_` Bearer token. The official package works the same inside Laravel — resolve it from the container or construct it in a service class.

## Prerequisites

1. A **verified domain** for your `from` address. ([guide](https://www.sendping.co/docs/domains/managing))
2. An **API key** (`mb_...`), stored in your `.env` as `SENDPING_API_KEY`. ([Authentication](https://www.sendping.co/docs/authentication))
3. Laravel 9+ (for the `Http` facade) — or any PHP 8.1+ with the `curl` and `json` extensions enabled. PHP 8.1 is the floor declared by the official `sendping/sendping` package; the raw-cURL path shown here also runs on PHP 8.0.

## 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('SENDPING_API_KEY'))
    ->acceptJson()
    ->post('https://www.sendping.co/api/emails', [
        'from' => 'Acme <hello@yourdomain.com>',
        'to' => ['delivered@test.sendping.co'],
        'subject' => 'Hello from Laravel',
        'html' => '<p>Sent with the Http facade.</p>',
    ]);

if ($response->failed()) {
    // SendPing 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('SENDPING_API_KEY');

$ch = curl_init('https://www.sendping.co/api/emails');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
        // cURL sends no User-Agent by default and the API rejects that with 403.
        'User-Agent: my-app/1.0',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'from' => 'Acme <hello@yourdomain.com>',
        'to' => ['delivered@test.sendping.co'],
        '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("SendPing {$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.sendping.co/docs/api/emails-get) or correlate [webhook](https://www.sendping.co/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

- Prefer the official package? [Send emails with PHP](https://www.sendping.co/docs/integrations/php) installs `sendping/sendping` and does the same send in three lines.
- See the full [Send Email API](https://www.sendping.co/docs/api/emails-send) reference.
- Read [Authentication](https://www.sendping.co/docs/authentication) for key permissions.
- Start from the [Quickstart](https://www.sendping.co/docs/quickstart).
