New Zenner
- Join Date:
- Feb 2009
- Posts:
- 101
- Plugin Contributions:
- 0
zen_mail sends wrong html body in subsequent emails due to global $current_page_base
Hi guys!
Zen Cart 1.5.5, PHP 7.4
Disclaimer: This is not standard Zen Cart code and only encounterable if you're heavily modifying things, so I do not class this as a bug, just a gotcha.
Just to share a moment of painful debugging to help others, I ran into some weirdness in the functions_email.php code where zen_build_html_email_from_template() is used to build the HTML portion of the email. It sets the global $current_page_base variable, which is then re-used in subsequent calls to the function. So if you are calling it from a standalone script to send emails using a different $module (email template) like this:
zen_mail(...some params..., 'default');
.. some other processing
zen_mail(...some params..., 'my_custom_template');
Inside zen_mail it calls zen_build_html_email_from_template($module, $block), which seems to be designed to be executed from a specific Zen Cart page being executed e.g. "shopping_cart" or "product", in which the very common variable $current_page_base would be set up. This doesn't hold up so well when run from your own custom script with a different global context where $current_page_base starts out undefined.
function zen_build_html_email_from_template($module='default', $content='') {
global $messageStack, $current_page_base;
if (NULL == $current_page_base) $current_page_base = $module;
... the template file to load from disk is then decided based on $current_page_base
The first time through it will set $current_page_base to the first module you pass, in my example above this is "default".
The second time through it will use "default" again as your email template, despite you passing in a different module name, in my example "my_custom_template".
This is easily avoided by unsetting $current_page_base in the global scope between calls to zen_mail.
zen_mail(...some params..., 'default');
.. some other processing
$current_page_base = null;
zen_mail(...some params..., 'my_custom_template');
Some very confused customers of ours were sent some very weird emails yesterday due to this :)
Cheers
Nick