# Laravel

> Send your first email from a Laravel controller by calling the SendPing API with the Http facade.

PHP has an official package — `composer require sendping/sendping` — and it works from any Laravel controller or service. The example below shows the dependency-free alternative instead: POST JSON to `https://www.sendping.co/api/emails` with the built-in [`Http` facade](https://laravel.com/docs/http-client), a wrapper over Guzzle. See [SDKs](https://www.sendping.co/docs/resources/sdks) for the package.

## Prerequisites

- A SendPing [API key](https://www.sendping.co/docs/api-keys/overview).
- A [verified domain](https://www.sendping.co/docs/domains/managing) to send from.

## 1. Add your API key

Add your key to the application's `.env` file and read it with the `env()` helper (or via a `config/` entry).

**.env**

```ini
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 2. Send email from a controller

Use the `Http` facade to POST the email body to the SendPing API. The easiest way to send is with the `html` field.

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

```php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Http;

class SendController extends Controller
{
    public function store(): JsonResponse
    {
        $response = Http::withToken(env('SENDPING_API_KEY'))
            ->post('https://www.sendping.co/api/emails', [
                'from' => 'Acme <onboarding@yourdomain.com>',
                'to' => ['delivered@test.sendping.co'],
                'subject' => 'hello world',
                'html' => '<strong>it works!</strong>',
            ]);

        $response->throw();

        return response()->json(['id' => $response->json('id')]);
    }
}
```

> **Note:** A successful call returns the created email's `id`. See every supported field in the [Send an email](https://www.sendping.co/docs/api/emails-send) reference.
