Sending Confirmation Mail doesn't work (using Merx)

Hi there,

I’m trying to send a confirmation mail after checkout in merx, but as soon as I’m using variables, the mail doesn’t get sent anymore.

This is the mail-part of my config.php:

return [
  'hooks' => [
        'ww.merx.completePayment:after' => function ($orderPage
        ) {
            sendConfirmationMail($orderPage);
          },
    ],
];

function sendConfirmationMail($orderPage)
{
  kirby()->email([
    'from' => 'test@test.com',
    'to' => (string)$orderPage->email(),
    'template' => 'confirmation',
    'data' => [
      'vorname' => $orderPage->vorname(),
      'nachname' => $orderPage->nachname(),
      'strasse' => $orderPage->strasse(),
      'nummer' => $orderPage->nummer(),
      'plz' => $orderPage->plz(),
      'ort' => $orderPage->ort(),
      'land' => $orderPage->land(),
      'price' => formatPrice($orderPage->cart()->getSum()),
      'url' => $orderPage->url(),
    ]  ]);
}

Here’s my checkout controller:

<?php

if (kirby()->request()->method() === 'POST') {
  try {
    $data = $_POST;
    $redirect = merx()->initializePayment($data);
    go($redirect);
  } catch (Exception $ex) {
    echo $ex->getMessage();
    dump($ex->getDetails());
  }
}
1 Like

What is $orderPage? A Kirby page object?

The in your array, you should use the field values instead of the field objects, e.g.

      'vorname' => $orderPage->vorname()->value(),

Yes, it’s a page object, when i use this “simplified” version, the process works:

function sendConfirmationMail($orderPage) {
    kirby()->email([
      'from' => 'test@test.com',
      'to' => (string)$orderPage->email(),
      'subject' => 'Thank’s for your order!',
      'body'=> 'Dear ' . $orderPage->vorname() . $orderPage->nachname() . ', you payed ' . formatPrice($orderPage->cart()->getSum()) . ', your invoice: ' . $orderPage->url(),
    ]);
  }

Does the email template exist?

yep, it’s inside
templates/emails/confirmation.html.php

And what is in this template?

<h1>Hallo <?= $vorname ?> <?= $nachname ?></h1>
<p>
    Vielen Dank für deine Bestellung.
</p>
<p>
    Versandadresse: <br>
    <?= $vorname ?> <?= $nachname ?> <br>
    <?= $strasse ?> <?= $nummer ?><br>
    <?= $plz ?> <?= $ort ?><br>
    <?= $land ?><br>
</p>
<p>
    Betrag:
    <?= $price ?>
</p>

<p>
    Deine Rechnung: <br>
    <?= $url ?>
</p>

Just to understand, does the simplified working example also work with the template i.e. when you remove the data array and just put some text into the template?

Tried this, but it doesn’t work:

config:

function sendConfirmationMail($orderPage)
{
  kirby()->email([
    'from' => 'mf@manuelfleig.com',
    'to' => (string)$orderPage->email(),
    'subject' => 'Thanks for your order!',
    'template' => 'confirmation',
  ]);
}

template:

<h1>Hallo </h1>
<p>
    Vielen Dank für deine Bestellung.
</p>

Hm, then it seems to be a problem with the template not being recognized for some reason, right?

Yes, after testing some variations it seems to be that:
as soon as the template comes into play it stops working.

Which Kirby version are you using?

version 3.6.2, php 8.1.0, nginx server (valet)

Could you test if it works with the latest version (3.6.6) please to rule out that we are trying to hunt an issue that has been fixed in the meantime.

1 Like

Yep, works like a charm with the latest version. As often, it’s the simple things :green_heart:

1 Like