# Send email from Laravel

> Send Laravel mail, Mailables and notifications through Vaartix with the vaartix mail driver, and verify webhooks.

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

The [PHP SDK](https://vaartix.com/docs/sdks/php/) registers a `vaartix` mail driver automatically (Laravel 12 and 13), so the `Mail` facade, Mailables, notifications and queued mail all go through Vaartix. 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
    ```

    The service provider is discovered automatically.

2.  **Configure the mailer**

    .env

    ```sh
    MAIL_MAILER=vaartix
    MAIL_FROM_ADDRESS=noreply@acme.in   # on a domain verified in Vaartix
    VAARTIX_API_KEY=vx_live_...
    VAARTIX_WEBHOOK_SECRET=whsec_...
    ```

    config/services.php

    ```php
    'vaartix' => [
        'key' => env('VAARTIX_API_KEY'),
        'webhook_secret' => env('VAARTIX_WEBHOOK_SECRET'),
    ],
    ```

    config/mail.php

    ```php
    'mailers' => [
        // ...
        'vaartix' => [
            'transport' => 'vaartix',
        ],
    ],
    ```

3.  **Send as usual**

    ```php
    Mail::to($user)->send(new OrderConfirmed($order));
    ```

    Laravel’s `->tag('welcome')` and `->metadata('order_id', 42)` become Vaartix tags, so you can filter by them in the dashboard. `SentMessage::getMessageId()` returns the Vaartix email ID.

To call the API directly, inject the client. It’s registered as a singleton from `services.vaartix.key`:

```php
public function store(Request $request, \Vaartix\Vaartix $vaartix)
{
    $vaartix->emails->send([
        'from' => 'Acme <orders@acme.in>',
        'to' => $request->user()->email,
        'subject' => 'Your order is confirmed',
        'html' => view('emails.order-confirmed', ['order' => $order])->render(),
    ], idempotencyKey: "order-confirmed-{$order->id}");
}
```

Until your domain is verified, send from `@test.vaartix.com` to `delivered@test.vaartix.com`: see [Test mode](https://vaartix.com/docs/sending/test-mode/).

## Receiving webhooks

routes/web.php

```php
use Illuminate\Http\Request;
use Vaartix\Exceptions\WebhookVerificationException;
use Vaartix\Webhook;

Route::post('/webhooks/vaartix', function (Request $request) {
    try {
        $event = Webhook::verify(
            $request->getContent(),
            $request->headers->all(),
            config('services.vaartix.webhook_secret'),
        );
    } catch (WebhookVerificationException $e) {
        abort(400);
    }
    if ($event['type'] === 'email.bounced') {
        // stop emailing $event['data']['recipient']
    }
    return response()->noContent();
});
```

Exclude this route from CSRF protection: webhook requests come from Vaartix, not from a browser.
