Answers.Body: <p>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.</p>
  <p>All of the failures in the queue are email addresses that have previously hard bounced in postmark.</p>
  <p>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.</p>
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-17T18:09:41.923Z
Answers.LastActivityDate: 2021-09-17T18:09:41.923Z
Answers.UserDisplayName: Josh Parylak
Answers.Body: <p>It sounds like you have either an order issue or a queue issue.</p>
  <p>What happens if you try to send that specific email from the order's edit screen?</p>
  <p>If it's a Twig/order email / syntax problem, you'll get an error.</p>
  <p>It sounds like you may have already tried this but when you preview an email from <code>Commerce -&gt; Store Settings -&gt; Emails</code>, you're previewing a random order however if you tack on <code>&amp;orderNumber=ORDER_NUMBER</code> at the end of the preview link, you can view a specific order.</p>
  <p>Alos, temporarily turning on <code>devMode</code> to see what might have gone wrong.</p>
  <p>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.</p>
  <p>Since it's easy to &quot;borrow&quot; code from other parts of your site (eg <code>cart</code>), make sure you've changed instances of <code>cart</code> to <code>order</code>.</p>
  <p>For example, when you preview an email in the CP,<code>cart</code> is available since you're ready logged in and wouldn't throw an error but when the queue worker runs, there's no <code>cart</code>, only  <code>order</code>.</p>
  <p>For example:</p>
  <pre><code>{{ lineItem.total|commerceCurrency(cart.currency) }}  
  </code></pre>
  <p>Should be:</p>
  <pre><code>{{ lineItem.total|commerceCurrency(order.currency) }}  
  </code></pre>
  <p>(Easy to miss.)</p>
  <p>To rule out the queue issue... how often/when are you running the <a href="https://craftcms.com/docs/3.x/config/config-settings.html#runqueueautomatically" rel="nofollow noreferrer">Craft queue</a>? It's possible the queue is timing out for some reason.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-13T12:06:59.467Z
Answers.Text: 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.
Answers.UserDisplayName: Josh Parylak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-14T13:24:42.423Z
Answers.Text: At this point, I would try locally with the same data and use xdebug to step through code and identify the failure.
Answers.UserDisplayName: sparkalow
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-11T18:42:42.987Z
Answers.LastActivityDate: 2021-09-11T18:42:42.987Z
Answers.UserDisplayName: RitterKnight
Body: <p>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.</p>
  <p>I've looked at the queue logs and I'm not seeing anything to go on here.</p>
  <p>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.</p>
  <p>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?</p>
  <pre><code>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-&gt;execute(Object(craft\queue\Queue))
  #1 /vendor/yiisoft/yii2-queue/src/cli/Queue.php(162): yii\queue\Queue-&gt;handleMessage('87238', 'O:35:&quot;craft\\com...', '300', '1')
  #2 /vendor/yiisoft/yii2-queue/src/cli/Command.php(146): yii\queue\cli\Queue-&gt;execute('87238', 'O:35:&quot;craft\\com...', '300', '1', '2054167')
  #3 [internal function]: yii\queue\cli\Command-&gt;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-&gt;runWithParams(Array)
  #6 /vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller-&gt;runAction('exec', Array)
  #7 /vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller-&gt;runAction('exec', Array)
  #8 /vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module-&gt;runAction('queue/exec', Array)
  #9 /vendor/craftcms/cms/src/console/Application.php(89): yii\console\Application-&gt;runAction('queue/exec', Array)
  #10 /vendor/yiisoft/yii2/console/Application.php(148): craft\console\Application-&gt;runAction('queue/exec', Array)
  #11 /vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application-&gt;handleRequest(Object(craft\console\Request))
  #12 /craft(23): yii\base\Application-&gt;run()
  #13 {main}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-15T02:53:04.720Z
Comments.Text: 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:`?
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-16T14:01:32.573Z
Comments.Text: @AugustMiller I've edited the question above and added the lines above Stack Trace from the queue log.
Comments.UserDisplayName: Josh Parylak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-16T16:52:32.983Z
Comments.Text: 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.).
Comments.UserDisplayName: August Miller
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-16T17:02:56.063Z
Comments.Text: 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.
Comments.UserDisplayName: Josh Parylak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-17T18:10:43.307Z
Comments.Text: @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.
Comments.UserDisplayName: Josh Parylak
Tags: <craft3><emails><plugin-craftcommerce3><status><queue>
Title: Specific emails failing in queue
ThreadText: # Specific emails failing in queue
  
  (Asked by: username_0 on 2021-09-10)
  
  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}
  
  ```
  
  **Tags:** craft3, emails, plugin-craftcommerce3, status, queue
  
  > **Comments:**
  > **username_1**: 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:`?  
  > **username_0**: @username_1 I've edited the question above and added the lines above Stack Trace from the queue log.  
  > **username_1**: 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.).  
  > **username_0**: 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.  
  > **username_0**: @username_1 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.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2021-09-17. Score: 2 | Accepted answer
  
  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.
  
  
  ---
  
  ### Answer by username_2 on 2021-09-11. Score: 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](https://craftcms.com/docs/3.x/config/config-settings.html#runqueueautomatically)? It's possible the queue is timing out for some reason.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_3**: At this point, I would try locally with the same data and use xdebug to step through code and identify the failure.  
  
  ---
  
  