# Send email from Symfony

A controller that sends a sign-in code and one that takes Mailset’s events, both routed with attributes. The webhook reads `$request->getContent()`, never `toArray()`, so the signature still matches.

## Install the PHP SDK

`composer require mailset-app/php` in the project, on PHP 8.1 or newer. Composer pulls nothing else in with it.

Composer:

```bash
composer require mailset-app/php
```

## Send your first email from Symfony

`SignInController` takes the client in its constructor, `new Client()` by default, which reads `MAILSET_KEY`. Its `#[Route]` reads the email with `$request->getPayload()->getString('email')`, sends with `deliverBy`, and returns the receipt’s id in a `JsonResponse`.

```php
use Mailset\Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

final class SignInController
{
    public function __construct(
        // One client for the app: it reads MAILSET_KEY.
        private Client $mailset = new Client(),
    ) {
    }

    #[Route('/sign-in/code', methods: ['POST'])]
    public function send(Request $request): JsonResponse
    {
        $to = $request->getPayload()->getString('email');
        $sent = $this->mailset->send([
            'from' => 'Acme <hello@example.com>',
            'to' => $to,
            'subject' => 'Your Acme code',
            'text' => 'Your code is 123456.',
            'deliverBy' => '10m',
        ]);

        return new JsonResponse(['id' => $sent->id]);
    }
}
```

## Verify webhooks with getContent()

The secret arrives through `#[Autowire(env: 'MAILSET_HOOK_SECRET')]` and the logger by autowiring. `__invoke` passes `$request->getContent()` and `$request->headers` to `Webhooks::verify`; a `MailsetException` answers 401 with its `errorCode`, a hard `BouncedEvent` is logged, and a 204 `Response` acknowledges the batch.

```php
use Mailset\Events\BouncedEvent;
use Mailset\MailsetException;
use Mailset\Webhooks;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\{
    Autowire,
};
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class MailsetWebhookController
{
    public function __construct(
        #[Autowire(env: 'MAILSET_HOOK_SECRET')]
        private string $secret,
        private LoggerInterface $logger,
    ) {
    }

    #[Route('/mailset', methods: ['POST'])]
    public function __invoke(Request $request): Response
    {
        try {
            // The signature covers the body as it came,
            // so read it raw: $request->toArray() or
            // ->request->all() loses the signed bytes.
            $events = Webhooks::verify(
                $request->getContent(),
                $request->headers,
                $this->secret,
            );
        } catch (MailsetException $error) {
            return new Response($error->errorCode, 401);
        }
        foreach ($events as $event) {
            if (!$event instanceof BouncedEvent) {
                continue;
            }
            if ($event->bounce->kind !== 'hard') {
                continue;
            }
            $to = $event->recipient;
            $this->logger->info("Stop writing to {$to}");
        }

        // 2xx once the events are kept. Until then
        // Mailset posts the batch again.
        return new Response(null, 204);
    }
}
```

In the SDK’s tests: run on real Symfony requests (http-foundation, routing), and checked by PHPStan.

## Send from your own domain

Your product sends as your own domain either way. What changes is where your team’s mail lives.

- **Sending only.** Your team’s mail stays at Google, Microsoft or wherever it is now. Mailset asks for DKIM, a bounce host and DMARC, and leaves your MX alone. [Send from a domain whose mail stays elsewhere](https://mailset.app/docs/recipes/sending-only)
- **Mailboxes too.** Your team’s mail moves to Mailset, with a real mailbox for every address on the domain and your product’s email on the same bill. [Set up a domain](https://mailset.app/guide/onboarding)

```bash
mailset domains add example.com --sending
mailset domains apply example.com --sending
mailset domains check example.com
mailset keys create --send --domain example.com --from hello@example.com --name "Acme API"
```

## What the PHP SDK’s build checks

Each line comes from the SDK’s own build, which fails when one stops being true.

- **PHP 8.1+**: The oldest its manifest allows (its manifest)
- **No dependencies**: Nothing else to install beside it (its manifest)
- **Typed throughout**: Readonly classes, checked by PHPStan (a test)

## Where to go from here

- [The PHP SDK](https://mailset.app/docs/sdk/php): Every task, from the client’s options to errors and retries.
- [Send email from PHP](https://mailset.app/send-email/php): The PHP guide, with no framework around it.
- [Webhooks](https://mailset.app/docs/webhooks): The events Mailset posts about your product’s email, and how to check a request’s signature.
- [Give sign-in codes a deadline](https://mailset.app/docs/recipes/deadlines): Use deliverBy so a code that would arrive late is never sent.
- [Retry without sending twice](https://mailset.app/docs/recipes/retries): Make a send safe to repeat with an idempotency key or a Message-ID.
- [Handle bounces](https://mailset.app/docs/recipes/bounces): Tell a dead address from a full inbox, and keep your list clean.
- [Send many messages at once](https://mailset.app/docs/recipes/batches): Up to 100 messages a request, each with its own answer.

## Product email on every plan

Both include 10,000 emails a month, your product’s and your team’s together. Past that it’s $0.40 per 1,000 with pay as you go. Every plan and price: https://mailset.app/pricing

## Sending from Symfony, asked and answered

### Does it replace Symfony Mailer?

It isn’t a Mailer transport. Call `$this->mailset->send()` where you would build an `Email` and hand it to the mailer.

### Why getContent() and not toArray()?

`toArray()` decodes the JSON and `->request->all()` reads form fields; both lose the bytes the signature covers. `getContent()` is the body as sent.

### Where does the secret come from?

`#[Autowire(env: ...)]` reads `MAILSET_HOOK_SECRET` from the environment, so it can live in `.env.local` or Symfony’s secrets vault. The CLI shows it once, when `mailset hooks set` makes the hook.

### Does the webhook route need a CSRF exemption?

No. Symfony checks CSRF tokens in forms, not on every POST, so a plain controller route takes Mailset’s request as it is.

## Try it on your own domain

7 days free, with everything on. Make a key, run the example above and watch the receipt come back. Start at https://mailset.app/start

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content