Mail Notifications
- How do I add a custom mail layout?
- How do I add attachments to a mailable?
- How do I set the priority of a mailable?
- How do I override the html content of a mailable?
- What is the preview mail action pipeline?
How do I add a custom mail layout?
To add a custom mail layout you can use the addLayout() method on the mail action:
\Aero\Events\Models\MailAction::addLayout('acme', 'Acme');
Then you can create the emails.acme twig view, which will be used to render your layout:
Acme {{ body | raw }}
How do I add attachments to a mailable?
You can attach files to a mailable by using the whenSendingAsMail callback:
\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',
]
);
});
This example uses the aerocargo/pdf-invoice module to generate a PDF Invoice for the order.
How do I set the priority of a mailable?
You can set the priority of a Mailable using the priority() method.
For example:
// 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);
});
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)
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 uses this to render the HTML of the email from the template, rather than the body:
\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:
\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);
});
This example is taken from the aerocommerce/email-builder module
Parameters
| Parameter | Type | Description |
$pipeline |
\Aero\Admin\Pipelines\PreviewMailAction |
The preview mail action pipeline. |
$next |
\Closure |
The next step in the pipeline. |
Methods
| Method | Description |
mailAction() |
Returns the temporary mail action instance being built. |
request() |
Returns the request. |