No Code Attached Yet
avatar BrainforgeUK
BrainforgeUK
29 Dec 2021

Steps to reproduce the issue

Try to send mail using a mail template which does not exist.

$mailer = new MailTemplate('com_bftest.invalidtemplateid', $language);
$mailer->addRecipient($templateData['emailto'], $templateData['emailtoname']);
$mailer->setReplyTo($templateData['replytoemail'], $templateData['replytoname']);
$mailer->addTemplateData($templateData);
$sent = $mailer->send();

Expected result

Sensible error message saying template does not exist

Actual result

Load of PHP notices and a 'Message body empty' warning.

System information (as much as possible)

Additional comments

Be better to add a getInstance() static method to MailTemplate which would allow this

$mailer = MailTemplate::getInstance('com_bftest.invalidtemplateid', $language);
if (empty($mailer))
{
     ... take some action ...
}
$mailer->addRecipient($templateData['emailto'], $templateData['emailtoname']);
$mailer->setReplyTo($templateData['replytoemail'], $templateData['replytoname']);
$mailer->addTemplateData($templateData);
$sent = $mailer->send();

Can do this as a workaround - downside MailTemplate::getTemplate() gets called twice.

$test = MailTemplate::getTemplate($templateId, $language);
if (empty($mail))
{
     ... take some action ...
}
$mailer = new MailTemplate('com_bftest.invalidtemplateid', $language);
$mailer->addRecipient($templateData['emailto'], $templateData['emailtoname']);
$mailer->setReplyTo($templateData['replytoemail'], $templateData['replytoname']);
$mailer->addTemplateData($templateData);
$sent = $mailer->send();
avatar BrainforgeUK BrainforgeUK - open - 29 Dec 2021
avatar BrainforgeUK BrainforgeUK - change - 29 Dec 2021
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 29 Dec 2021
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 29 Dec 2021
avatar PhilETaylor
PhilETaylor - comment - 29 Dec 2021

The problem is that, mails can come from anywhere, Admin, Frontend, CLI etc... So the correct thing would be an Exception, but that is too hard on a "user trying to send a contact form, formatted by a non existing mail template) and equally, in /administrator/ a "error" message can be displayed in the admin console, and for CLI an exception...

To display those messages has to be done outside of the MailTemplate class - which is only concerned about compiling and sending the mail.

therefore I would just say that $mailer->send() should return false, with no other error messages

or just throw an Exception and expect 3PD to deal with catching it?

avatar PhilETaylor
PhilETaylor - comment - 30 Dec 2021

Simple approach #36489

avatar richard67 richard67 - change - 30 Dec 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-12-30 08:07:09
Closed_By richard67
avatar richard67 richard67 - close - 30 Dec 2021
avatar richard67
richard67 - comment - 30 Dec 2021

Closing as having a pull request. Please test #36489 . Thanks in adcance.

Add a Comment

Login with GitHub to post a comment