Magento 2 Error 500 – Email Trigger Issue

emailhttp-error-500magento2

We have this weird issue on one of our websites.

When customers try to buy something they get an error 500 when they complete the order, the order is registered but they get an error 500 and no email is sent to the customer.

This also happens when a new customer is registering, so when they hit submit they get an error 500 and the registration is stored in Magento.

We also get error 500 in Magento when we try to click on the "reset password" button.

The reason why I think it has something to do with emails is that all of these actions should trigger an email to the customer. However, We have tested the email and it works.

This is what i get in the error logs:

==> var/log/exception.log <==
[2021-10-11 14:47:59] main.CRITICAL: Notice: Undefined index: directiveName in /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/framework/Filter/DirectiveProcessor/SimpleDirective.php on line 68 {"exception":"[object] (Exception(code: 0): Notice: Undefined index: directiveName in /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/framework/Filter/DirectiveProcessor/SimpleDirective.php on line 68 at /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/framework/App/ErrorHandler.php:61)"} []

==> ../../logs/php-error.log <==

[11-Oct-2021 14:47:59 UTC] PHP Fatal error:  Uncaught TypeError: Return value of Magento\Framework\Filter\DirectiveProcessor\DependDirective::process() must be of the type string, object returned in /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/framework/Filter/DirectiveProcessor/DependDirective.php:47

Stack trace:

#0 /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/framework/Filter/Template.php(184): Magento\Framework\Filter\DirectiveProcessor\DependDirective->process(Array, Object(Magento\Email\Model\Template\Filter), Array)

#1 /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/module-email/Model/Template/Filter.php(1080): Magento\Framework\Filter\Template->filter('<!-- End Conten...')

#2 /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/module-email/Model/AbstractTemplate.php(369): Magento\Email\Model\Template\Filter->filter('<!-- End Conten...')

#3 /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/module-email/Model/AbstractTemplate.php(247): Magento\Email\Model\AbstractTempla in /chroot/home/a96f0e0b/stores.etailhandel.no/html/vendor/magento/framework/Filter/DirectiveProcessor/DependDirective.php on line 47
    <!-- End Content -->
                    </td>
                </tr>
                <tr>
                    <td class="footer">
                        <table>
                            <tr>
                                <td>
                                    <p><a href="https://cobi.no/om-cobi-norge">Om Cobi Norge</a></p>
                                    <p><a href="https://cobi.no/nyheter.html">Forhandlernytt</a></p>
                                </td>
                                <td>
                                    {{depend store_phone}}
                                        <p class="phone">
                                            {{trans '<a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}
                                        </p>
                                    {{/depend}}
                                    {{depend store_hours}}
                                        <p class="hours">
                                            Ã…pningstider: <br />{{<span class="no-link">%store_hours</span>.' store_hours=$store_hours |raw}}
                                        </p>
                                    {{/depend}}
                                </td>
                                <td>
                                    <p class="address">
                                        Kabelgata 8<br/>
                                        0580 Oslo<br/>
                                    </p>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<!-- End wrapper table -->
</body>

Best Answer

I had this same problem.

Although I don't have access to your store to evaluate your specific case, here are some hypotheses that I encountered when working through this on my own store(s).


Keep in mind: Template issues don't have to exist directly in your top-level email template.
The issue can manifest when a child template contains the issue that is being called-in from your top-level template.
Nonetheless: Be confident that all template issues can be resolved within Magento's Admin and do not require changes to the codebase.


Hypothesis #0 (Very likely)

In the footer_template shown in your Question. There is an inconsistency.

In the first usage of {{trans ''}} regarding store_phone, it looks desired:

{{trans '<a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}

However, in the second usage of {{trans ''}} regarding store_hours, the syntax looks malformed. It seems the opening trans ' after the opening brackets {{ is missing:

{{<span class="no-link">%store_hours</span>.' store_hours=$store_hours |raw}}

Hypothesis #1 (Very likely)

You need to close your template tags!

{{depend some_var}}Some Content{{/depend}}

In my codebase, this was noticed on email templates containing {{template config_path="design/email/footer_template"}} because the footer template we created in Magento's Admin didn't close a depend tag {{depend store_phone}} with a corresponding {{/depend}}.


Hypothesis #2 (Less likely)

A non-existent variable or a variable containing no data.

Although I wasn't able to test this other hypothesis at the time, I became suspicious that using a variable for which there is no data for could also cause this error even if your template tags are properly terminated. However this would be such a silly oversight by Magento that I became doubtful it could be related to the issue.

For example, I thought this could throw an error when I didn't have my phone number saved into Magento Admin's store configuration (at path general/store_information/phone):

{{depend store_phone}}
    Call us: {{store_phone}}
{{/depend}}

Edit answer - added #3:

The original answer was quite good, so I decided to just add my information found as part of it. I am adding an additional possible reason when this error might appear.

Hypothesis #3 (Very likely)

- Nested {{depends}} or {{if}} not supported.

Regarding Hypothesis #2, this should not really going to be a problem if you have a missing value, or missing directive, it will simply output:

<p>Call us: {{store_phone}}</p>

In my testing it just outputs the variable as part of the text.

What this Hypothesis #2 helped me with was to think of {{depend}} and {{if}} together.

To put it simply, it does not appear to be possible to nest multiple same type conditions into each other:

This will also give the same error as original poster:

{{depend order_data.is_not_virtual}}
   <td class="method-info">
      <h3>{{trans "Shipping Method"}}</h3>
      {{depend shipping_msg}}
         <p>{{var shipping_msg}}</p>
      {{/depend}}
    </td>
{{/depend}}

However this should work:

{{depend order_data.is_not_virtual}}
   <td class="method-info">
      <h3>{{trans "Shipping Method"}}</h3>
      {{if shipping_msg}}
         <p>{{var shipping_msg}}</p>
      {{/if}}
    </td>
{{/depend}}

I had an extra {{depend}} inside this {{depend}} for a new custom directive (not shown here, using existing variable directive as example) and it failed since I thought depend and if are used differently (functionally); but as described above it does not appear to be the case really. I do not know what the difference of {{depend}} and {{if}} is functionally, but they appear to be interchangeable.

Summary of all of these errors.

Based on my issue, I assume the regex matching used ensures that there are opening and matching closing entity, so a nested 'condition' entity will partially cut-off the string.

Hypothesis #1 and #2 also seems to reinforce the idea that all of these issues are related to regex matching failure because of unexpected end / missing / malformed 'tags'

Related Topic