# Mail Notifications

# How do I add a custom mail layout?

To add a custom mail layout you can use the `addLayout()` method on the mail action:

```php
\Aero\Events\Models\MailAction::addLayout('acme', 'Acme');
```

Then you can create the `emails.acme` twig view, which will be used to render your layout:

```twig
Acme {{ body | raw }}
```

# How do I add attachments to a mailable?

You can attach files to a mailable by using the `whenSendingAsMail` callback:

```php
\Aero\Cart\Events\OrderConfirmation::whenSendingAsMail(function (\Aero\Events\Actions\Mailable $mailable, \Aero\Cart\Events\OrderConfirmation $event) {
    $pdf = \Aerocargo\PdfInvoices\Invoice::forOrder($event->order, 'invoice');

    $mailable->attachData(
        $pdf->output(),
        "{$event->order->reference}-invoice.pdf",
        [
            'mime' => 'application/pdf',
        ]
    );
});
```

<p class="callout info">This example uses the [`aerocargo/pdf-invoice`](https://agora.aerocommerce.com/packages/aerocargo/pdf-invoices) module to generate a PDF Invoice for the order.</p>

# How do I set the priority of a mailable?

You can set the priority of a Mailable using the `priority()` method.

For example:

```php
// For a specific event
\Aero\Cart\Events\OrderConfirmation::whenSendingAsMail(function (\Aero\Events\Actions\Mailable $mailable, \Aero\Cart\Events\OrderConfirmation $event) {
    $mailable->priority(1);
});

// For all order events
\Aero\Cart\Events\AbstractOrderEvent::whenSendingAsMail(function (\Aero\Events\Actions\Mailable $mailable, \Aero\Cart\Events\AbstractOrderEvent $event) {
    $mailable->priority(1);
});

// For all events
\Aero\Events\ManagedEvent::whenSendingAsMail(function (\Aero\Events\Actions\Mailable $mailable, \Aero\Events\ManagedEvent $event) {
    $mailable->priority(1);
});
```

<p class="callout info">Email priority does not control how quickly it is delivered. It is a sign of importance to the recipient's email client (it may not be honoured by every provider)</p>

# How do I override the html content of a mailable?

You can set the HTML of a Mailable using the `html()` method.

For example, the [email builder](https://agora.aerocommerce.com/packages/aerocommerce/email-builder) uses this to render the HTML of the email from the `template`, rather than the `body`:

```php
\Aero\Events\ManagedEvent::whenSendingAsMail(function (\Aero\Events\Actions\Mailable $mailable, \Aero\Events\ManagedEvent $event) {
    $action = $mailable->action();

    if (! empty($action->body)) {
        return;
    }

    $mailable->html(
        \Aero\EmailBuilder\Facades\EmailBuilder::renderTemplate((array) $event, $action->getKey())
    );
});
```

# What is the preview mail action pipeline?

The Preview Mail Action Pipeline is used to build a temporary `\Aero\Events\Models\MailAction` instance from request data to be previewed.

For example, you can add custom fields to the `\Aero\Events\Models\MailAction` instance:

```php
\Aero\Admin\Pipelines\PreviewMailAction::extend(function (\Aero\Admin\Pipelines\PreviewMailAction $pipeline, \Closure $next) {
    \Aero\EmailBuilder\Facades\EmailBuilder::boot();

    $data = $pipeline->request()->validated();

    $version = \Aero\EmailBuilder\Helpers\PreviewVersion::get($data);

    $template = \Aero\EmailBuilder\Facades\EmailBuilder::getTemplate($data['id'], $version)->toArray();

    $pipeline->mailAction()->fill([
        'template_header' => $template['header'] ?? null,
        'template_body' => $template['body'] ?? null,
        'template_footer' => $template['footer'] ?? null,
        'template_settings' => $template['settings'] ?? null,
    ]);

    return $next($pipeline);
});
```

<p class="callout info">This example is taken from the [`aerocommerce/email-builder`](https://agora.aerocommerce.com/packages/aerocommerce/email-builder) module</p>

---

### Parameters

<table border="1" id="bkmrk-parameter-type-descr" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 33.3731%;"></col><col style="width: 33.3731%;"></col><col style="width: 33.3731%;"></col></colgroup><tbody><tr><td>**Parameter**</td><td>**Type**</td><td>**Description**</td></tr><tr><td>`$pipeline`</td><td>`\Aero\Admin\Pipelines\PreviewMailAction`</td><td>The preview mail action pipeline.</td></tr><tr><td>`$next`</td><td>`\Closure`</td><td>The next step in the pipeline.</td></tr></tbody></table>

### Methods

<table border="1" id="bkmrk-method-description-i" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50.0596%;"></col><col style="width: 50.0596%;"></col></colgroup><tbody><tr><td>**Method**</td><td>**Description**</td></tr><tr><td>`mailAction()`</td><td>Returns the temporary mail action instance being built.</td></tr><tr><td>`request()`</td><td>Returns the request.</td></tr></tbody></table>