# Symfony

> Send your first email from a Symfony controller using the Symfony HttpClient against the SendPing API.

PHP has an official SDK — `composer require sendping/sendping` — and it works in any Symfony controller or service (`SendPing::client($key)`, then `$sendping->emails->send([ ... ])`). The example below instead shows the dependency-free route, POSTing JSON to `https://www.sendping.co/api/emails` with Symfony's [HttpClient](https://symfony.com/doc/current/http_client.html) component, which you can inject into any controller or service.

## 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.
- A Symfony application.

## 1. Install the HTTP client

Install the Symfony HttpClient component with Composer.

**Composer**

```bash
composer require symfony/http-client
```

## 2. Configure your API key

In your `.env.local` file, which you can create if needed, add your SendPing API key. Never commit this file.

**.env.local**

```sh
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 3. Send your first email

Inject the `HttpClientInterface` into a controller and POST the email body. The easiest way to send is with the `html` field.

**src/Controller/EmailController.php**

```php
<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class EmailController
{
    public function __construct(
        private readonly HttpClientInterface $client,
    ) {
    }

    #[Route('/send', name: 'send_email')]
    public function send(): JsonResponse
    {
        $response = $this->client->request('POST', 'https://www.sendping.co/api/emails', [
            'auth_bearer' => $_ENV['SENDPING_API_KEY'],
            'json' => [
                'from' => 'Acme <onboarding@yourdomain.com>',
                'to' => ['delivered@test.sendping.co'],
                'subject' => 'Hello world',
                'html' => '<strong>it works!</strong>',
            ],
        ]);

        return new JsonResponse($response->toArray());
    }
}
```

> **Note:** A successful call returns the created email's `id`. See [Send an email](https://www.sendping.co/docs/api/emails-send) for the complete request body.
