# Send email with Symfony Mailer

> Send Symfony Mailer email through Vaartix with a MAILER_DSN and the transport factory from the PHP SDK.

Source: https://vaartix.com/docs/guides/symfony/

Since [vaartix/vaartix-php](https://vaartix.com/docs/sdks/php/) 0.3.0, Symfony Mailer can send through Vaartix with a `MAILER_DSN`, so `MailerInterface`, `TemplatedEmail`, notifications and Messenger-queued mail all go through Vaartix. It needs `symfony/mailer` 6.2 or newer. The source is on GitHub at [vaartix-mail/vaartix-php](https://github.com/vaartix-mail/vaartix-php) and the package is on [Packagist](https://packagist.org/packages/vaartix/vaartix-php).

1.  **Install the SDK**

    ```sh
    composer require vaartix/vaartix-php symfony/mailer
    ```

2.  **Set the DSN**

    .env.local

    ```dotenv
    MAILER_DSN=vaartix+api://vx_your_api_key@default
    ```

    `default` means `https://api.vaartix.com`. Add `?timeout=10` to change the request timeout (seconds), for example `vaartix+api://vx_your_api_key@default?timeout=10`. If your API key contains `+` or `/`, URL-encode them in the DSN (`%2B` and `%2F`).

3.  **Register the transport factory**

    config/services.yaml

    ```yaml
    services:
        Vaartix\Symfony\VaartixTransportFactory:
            parent: mailer.transport_factory.abstract
            tags: ['mailer.transport_factory']
    ```

4.  **Send a Symfony email**

    ```php
    use Symfony\Component\Mailer\MailerInterface;
    use Symfony\Component\Mime\Email;

    public function confirm(MailerInterface $mailer): Response
    {
        $mailer->send(
            (new Email())
                ->from('Acme <orders@acme.in>') // on a domain verified in Vaartix
                ->to('priya@example.com')
                ->subject('Order confirmed')
                ->html('<p>Thanks for your order!</p>'),
        );
        // ...
    }
    ```

## Without the framework

Build the transport from the same DSN:

```php
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Vaartix\Symfony\VaartixTransportFactory;

$transport = (new Transport([new VaartixTransportFactory()]))
    ->fromString('vaartix+api://vx_your_api_key@default');
$mailer = new Mailer($transport);
```

Or create the transport directly, reading `VAARTIX_API_KEY` from the environment:

```php
$mailer = new Mailer(new Vaartix\Laravel\VaartixTransport(new Vaartix\Vaartix()));
```

To verify webhooks, use `Vaartix\Webhook::verify()` as shown in the [PHP SDK](https://vaartix.com/docs/sdks/php/#webhooks) docs.
