Sending emails
Learn how to use hooks to send confirmation mails to the customer.
Merx comes with several hooks that allow you to hook into processes.
Use the createOrder:after hook to send a mail to the customer to inform about the succeeded order.
<?php
return [
'hooks' => [
'wagnerwagner.merx.createOrder:after' => function (\Wagnerwagner\Merx\OrderPage $orderPage) {
$name = $orderPage->given_name() . ' ' . $orderPage->family_name();
kirby()->email([
'from' => 'info@shop.test',
'fromName' => $orderPage->site()->title(),
'to' => [(string)$orderPage->email() => $name],
'subject' => 'Thanks for your order!',
'body' => 'Dear ' . $name . ', thank you for your order of ' . $orderPage->total(),
]);
},
],
];
The hook runs for every order, paid or not: an invoice or SEPA Direct Debit order is still waiting for its payment. Check $orderPage->isPaid() before the mail says the order is paid.
Read Kirby’s guide about emails to learn more about sending mails.
Best practices
Use email presets
It’s recommended to use mail presets. This has the advantage that you can define the sender email address and a mail template globally in the config file.
<?php
return [
'email' => [
'presets' => [
'createOrder' => [
'from' => 'info@shop.test',
'template' => 'create-order',
],
],
],
'hooks' => [
'wagnerwagner.merx.createOrder:after' => function (\Wagnerwagner\Merx\OrderPage $orderPage) {
kirby()->email('createOrder', [
'to' => (string)$orderPage->email(),
'fromName' => $orderPage->site()->title(),
'subject' => 'Thanks for your order!',
'data' => [
'orderPage' => $orderPage,
],
]);
},
],
];
<?php /** @var \Wagnerwagner\Merx\OrderPage $orderPage */ ?>
Dear <?= $orderPage->given_name()->escape() ?> <?= $orderPage->family_name()->escape() ?>,
Thank you for your order of <?= $orderPage->total() ?>.
Find all the details of your order at
<?= $orderPage->url() ?>
When you create html emails, always use $field->escape() for fields with user generated content to prevent XSS attacks.