Hello Kunena team,
After upgrading to Kunena 7.1.0, the scheduled task "Kunena - Send the mail notifications queued" fails when it runs from the Joomla CLI (php cli/joomla.php scheduler:run, triggered by cron). The task log shows:
ERROR Task> Queued notification #1 could not be sent: Call to undefined method Joomla\CMS\Application\ConsoleApplication::getTemplate()
WARNING Task> One or more queued notifications could not be sent and will be retried.
Environment: Kunena 7.1.0, Joomla 6.2, PHP 8.5, task executed via CLI cron.
ISSUE 1 – Fatal error in KunenaTemplate::getInstance() under CLI
Stack trace:
libraries/kunena/src/Template/KunenaTemplate.php:483 KunenaTemplate::getInstance()
libraries/kunena/src/Factory/KunenaFactory.php:76 KunenaFactory::getTemplate()
libraries/kunena/src/Layout/KunenaBase.php:709 KunenaBase::factory()
libraries/kunena/src/Forum/Message/KunenaMessage.php:1963 KunenaMessage->attachEmailBody()
It happens when the configured Kunena template no longer exists. In our case the site was upgraded from older versions and the configuration still had template = "crypsis". The code then takes the fallback branch:
// If template xml doesn't exist, raise warning and use aurelia instead
$file = JPATH_THEMES . "/{$app->getTemplate()}/html/com_kunena/template.php";
$app is Factory::getApplication(), which is a ConsoleApplication under CLI and has no getTemplate() method. The $file value assigned here is never used: it is overwritten inside the following isClient('site') block, or ignored otherwise. Removing the line, or guarding it with $app->isClient('site'), fixes the issue. Setting Aurelia as the default template also works around it.
It might also be worth making the upgrade process reset an obsolete template value (such as crypsis) to aurelia.
ISSUE 2 – Wrong message links in emails sent from CLI
Once issue 1 is worked around, the emails are sent, but the message links are wrong:
- With live_site empty:
joomla.invalid/set/by/console/applicatio...=2447&Itemid=0#10614
- With live_site set:
www.example.comcli/index.php?option=com_...c&...&Itemid=0#10614
(no slash, cli/ path, non-SEF, Itemid=0)
The email layouts rebuild the link from the current request instead of using the URL that was stored in the queue:
- components/com_kunena/template/aurelia/layouts/email/subscription/default.php lines 25 and 204
- components/com_kunena/template/aurelia/layouts/email/subscription/moderator.php line 25
$this->messageLink = Uri::getInstance()->toString(array('scheme', 'host', 'port')) . $this->message->getUrl(null, false);
KunenaMessage::processNotification() already computes the correct permalink during the web request and stores it in #__kunena_notifications_mailsqueue.url. That value is passed to the layout as $this->messageUrl by attachEmailBody(), but the layouts never use it. A possible fix:
$this->messageLink = $this->messageUrl?: Uri::getInstance()->toString(array('scheme', 'host', 'port')) . $this->message->getUrl(null, false);
Line 204 of default.php should also use $this->messageLink instead of recomputing the URL. I'd also suggest escaping the link when it is output in the HTML (htmlspecialchars($this->messageLink, ENT_QUOTES, 'UTF-8')).
Best regards,
Jose