# Symfony

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

The `mailblastr` SDK is for Node.js — PHP users send email by POSTing JSON to `https://api.mailblastr.com/emails`. The example below uses Symfony's [HttpClient](https://symfony.com/doc/current/http_client.html) component, which you can inject into any controller or service.

## Prerequisites

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/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 MailBlastr API key. Never commit this file.

**.env.local**

```sh
MAILBLASTR_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://api.mailblastr.com/emails', [
            'auth_bearer' => $_ENV['MAILBLASTR_API_KEY'],
            'json' => [
                'from' => 'Acme <onboarding@yourdomain.com>',
                'to' => ['delivered@example.com'],
                '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.mailblastr.com/docs/api/emails-send) for the complete request body.
