Problems with e-mail

I’m encountering an issue with a shop website using Merx, but I don’t believe this is an error related to the Merx plugin, hence I’m posting it in the general forum. The site is running on localhost but should be going live soon.

There’s a function in my config.php to send automated e-mails whenever the ww.merx.completePayment:after hook is fired:

// config.php
$dotenv = new \Dotenv\Dotenv(__DIR__ . '/../..');
$dotenv->load();
// Login credentials etc saved at root in an .env file => i.e $_ENV['EMAIL_HOST'],

if (!function_exists('sendConfirmationMail')) {
  function sendConfirmationMail($orderPage, $urlWithHash) {
    try {
      kirby()->email([
        'from' => 'test@example.com',
        'to' => (string)$orderPage->email(),
        'subject' => 'Thanks for your order!',
        'body'=> 'Dear ' . $orderPage->name() . ', you paid ' . formatPrice($orderPage->cart()->getSum()) . '. Order summary: ' . $urlWithHash,
      ]);
      kirby()->email([
        'from' => 'test@example.com',
        'to' => 'notifications@example.com',
        'subject' => 'New Order',
        'body' => 'You have outstanding orders. Please check the panel: ' . kirby()->site()->url() . '/panel'
      ]);
    } catch(Exception $error) {
      echo $error;
    }
  }
};

return [
    // other stuff
    'hooks' => [
      'ww.merx.completePayment:after' => function ($orderPage) {
        $hash = bin2hex(random_bytes(16));
        $urlWithHash = url($orderPage->url(), ['params' => ['q' => $hash]]);
        $orderPage->update([
          'hash' => $hash,
        ]);
        sendConfirmationMail($orderPage, $urlWithHash);
        go($urlWithHash);
      },
    ], 
    'email' => [
      'transport' => [
        'type' => 'smtp',
        'host' => $_ENV['EMAIL_HOST'],
        'port' => $_ENV['EMAIL_PORT'],
        'security' => true,
        'auth' => true,
        'username' => $_ENV['EMAIL_USER'],
        'password' => $_ENV['EMAIL_PASS'],
      ]
    ],
]

Whenever I complete a payment now (whenever the hook is called), the server gets stuck in an endless loading, resulting in a 504 gateway timeout.

The weird thing is that everything has already been tested and working correctly up until the day before yesterday, and I don’t think I’ve made any substantial changes to the file.

Without specifying email transport configuration, everything runs correctly, but emails never arrive, which leads me to believe that there must be something wrong in that part – but I have no clue where to start debugging. If the mail server is down, for example, shouldn’t there be some sort of error message thrown? Appreciate any hints or ideas…

Since you call the code in a hook, you won’t get an error message. I’d try this in a function to see if it works there or throws an error, or use a debugging tool like Xdebug to see where it goes wrong.

There was a post some time ago which is about the security option: Kirby->Email / SMTP Error (Timeout?) - #10 by jango

Seems like 'security' => true is not correct, but 'security' => 'ssl'. Check also if you are using the correct port.

Thanks! This fixed it. I did search the forum, but didn’t see this thread…

Edit: now that I think about it, I did upgrade Kirby before this error occurred. If it’s really the case that 'security' => true doesn’t work anymore with 3.4 upwards, like stated in the linked thread, maybe it might make sense mentioning that in the docs (something like “it is recommended to explicitly set ‘ssl’ or ‘tls’” instead of “you can set ssl or tls”)

Makes sense, didn’t think about that… Maybe I’ll put the function on the redirect page then, instead of inside the hook.

Thanks both for your help!

So much for “I didn’t make any changes…” :wink:

1 Like