1

I have craft 3.7.11 with commerce 3.4.2 installed. The majority of site emails send correctly, but every few days, one order status change email fails. The prior emails for these orders send correctly, it is always on the same status change, a change to 'shipped' that triggers an email with tracking information. It happens for multiple customers/products/orders, but always this status change email, and only about 5% of the time. I've loaded the preview template for the email with the order in question, and there are no errors on the template. There is no complex twig in any of the email fields, just the order reference number in the subject line. Retrying the email in the queue always fails again.

I've looked at the queue logs and I'm not seeing anything to go on here.

I've gone through the craft documentation's check list for email failure and everything seems to be in order. Again, most of the emails for the site work fine, and 95% of the time, this status change email works fine as well and it doesn't seem to be an issue of the order data causing a template error.

I'm adding the stack trace from the queue log. The web.log doesn't have any errors in it from the time the email failed. Can anyone point me in the right direction from here?

2021-09-09 14:32:02 [-][-][-][info][craft\queue\QueueLogBehavior::beforeExec]  [87238] Sending email for order #278405 (attempt: 1, pid: 2054167) - Started
2021-09-09 14:32:03 [-][-][-][error][craft\commerce\services\Emails::sendEmail] Commerce email “Shipping Confirmation” could not be sent for order “382b888”.
2021-09-09 14:32:03 [-][-][-][error][craft\queue\QueueLogBehavior::afterError]  [87238] Sending email for order #278405 (attempt: 1, pid: 2054167) - Error (time: 0.355s): Commerce email “Shipping Confirmation” could not be sent for order “382b888”.
2021-09-09 14:32:03 [-][-][-][error][craft\commerce\errors\EmailException] craft\commerce\errors\EmailException: Commerce email “Shipping Confirmation” could not be sent for order “382b888”. in /var/www/[site]/vendor/craftcms/commerce/src/queue/jobs/SendEmail.php:54
Stack trace:
#0 /vendor/yiisoft/yii2-queue/src/Queue.php(246): craft\commerce\queue\jobs\SendEmail->execute(Object(craft\queue\Queue))
#1 /vendor/yiisoft/yii2-queue/src/cli/Queue.php(162): yii\queue\Queue->handleMessage('87238', 'O:35:"craft\\com...', '300', '1')
#2 /vendor/yiisoft/yii2-queue/src/cli/Command.php(146): yii\queue\cli\Queue->execute('87238', 'O:35:"craft\\com...', '300', '1', '2054167')
#3 [internal function]: yii\queue\cli\Command->actionExec('87238', '300', '1', '2054167')
#4 /vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#5 /vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams(Array)
#6 /vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('exec', Array)
#7 /vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('exec', Array)
#8 /vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('queue/exec', Array)
#9 /vendor/craftcms/cms/src/console/Application.php(89): yii\console\Application->runAction('queue/exec', Array)
#10 /vendor/yiisoft/yii2/console/Application.php(148): craft\console\Application->runAction('queue/exec', Array)
#11 /vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(craft\console\Request))
#12 /craft(23): yii\base\Application->run()
#13 {main}
5
  • What error is reported in the Queue/Job utility itself? The stack trace will be useful, but without knowing what part of the execute() method actually failed, we can only speculate—is there anything else in the 2–3 lines above Stack trace:? Commented Sep 15, 2021 at 2:53
  • @AugustMiller I've edited the question above and added the lines above Stack Trace from the queue log. Commented Sep 16, 2021 at 14:01
  • Hey, thanks—this is helpful. This particular error message is only emitted when there's an underlying failure with the Mailer component or adapter. Unfortunately, the Mailer doesn't throw on its own, so the failure mode is really generic (basically, "couldn't send"). What email provider are you using? It could be something as simple as it getting falsely flagged as spam and rejected by the SMTP server or API (i.e. due to something in the text, a URL, etc.). Commented Sep 16, 2021 at 16:52
  • We use Postmark. I'm not seeing any failures in Postmark that seem to relate to these failures, so I had assumed the failure was happening before we got to postmark. These emails do send tracking links, so I suppose it's possible certain tracking links are tripping it? I'll look a little deeper on the postmark end and compare the content of the failed emails. Commented Sep 16, 2021 at 17:02
  • @AugustMiller Thank you, your comment led me to Postmark, where I was finally able to diagnose what was going on. I've added the full story as an answer. Commented Sep 17, 2021 at 18:10

2 Answers 2

2

I'm going to lay out the answer here, in case someone else runs into someone else's issue. We are using Postmark to process emails.

All of the failures in the queue are email addresses that have previously hard bounced in postmark.

The reason it's always the shipping confirmation email has nothing to do with the shipping confirmation email, but the fact that it's the second email these addresses are sent.

They are sent their order confirmation emails. Postmark processes those emails, so they clear the craft queue. Then those emails bounce, so postmark flags those email addresses. When the second email, the shipping confirmation, is sent Postmark rejects it, causing it to fail in the queue.

1

It sounds like you have either an order issue or a queue issue.

What happens if you try to send that specific email from the order's edit screen?

If it's a Twig/order email / syntax problem, you'll get an error.

It sounds like you may have already tried this but when you preview an email from Commerce -> Store Settings -> Emails, you're previewing a random order however if you tack on &orderNumber=ORDER_NUMBER at the end of the preview link, you can view a specific order.

Alos, temporarily turning on devMode to see what might have gone wrong.

That 5% of the time sounds like tracking information may not be available or some other issue? I would double check your code for variables that wouldn't be available when the queue worker runs.

Since it's easy to "borrow" code from other parts of your site (eg cart), make sure you've changed instances of cart to order.

For example, when you preview an email in the CP,cart is available since you're ready logged in and wouldn't throw an error but when the queue worker runs, there's no cart, only order.

For example:

{{ lineItem.total|commerceCurrency(cart.currency) }}  

Should be:

{{ lineItem.total|commerceCurrency(order.currency) }}  

(Easy to miss.)

To rule out the queue issue... how often/when are you running the Craft queue? It's possible the queue is timing out for some reason.

2
  • When previewing in the Store Settings using the order number, the template loads without errors. That was my first thought. I've checked the failing order, and it isn't missing any data. Double checked, and I am using order in the template, so good there. The queue is run frequently because it's also used to update the products from an external system using feed me. I thought other failures might be the issue, but re-running with just the failed email still fails. Commented Sep 13, 2021 at 12:06
  • At this point, I would try locally with the same data and use xdebug to step through code and identify the failure.
    – sparkalow
    Commented Sep 14, 2021 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.