No Code Attached Yet
avatar nikosdion
nikosdion
30 May 2022

Steps to reproduce the issue

  • Make sure your PHP cannot send emails with mail(). For example, edit your php.ini and set
    [mail function]
    sendmail_path=/usr/local/foobar
  • Go to System, Global Configuration, Server and set up your Mail options. I am using MailHog on my local server (SMTP on localhost:1025) for my local testing. You can use whatever works for you.
  • Make sure your site can send emails using the “Send Test Email” button.
  • Click on Save & Close.
  • Go to System, Mail Templates, Options.
  • Set “Per Template Mail Settings” to Yes.
  • Click on Save & Close.
  • Go to System, Mail Templates.
  • Click on the “Global Configuration: Test Mail” item.
  • Go to the Options tab.
  • Set Mail Settings to No.
  • Click on Save & Close.
  • Go to System, Global Configuration, Server tab.
  • Click on the “Send Test Email” button.

Expected result

A test email is sent using the Global Configuration settings.

Actual result

Error message:

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

System information (as much as possible)

  • Joomla 4.1.4 and 4.2-dev (pulled and updated as of 30th May, 2022 at 23:50 EEST)
  • PHP versions 7.4, 8.0 and 8.1
  • Tested on Windows 10, Windows 11, macOS Monterey and Ubuntu Linux 22.04

It really doesn't matter because it's a verifiable bug.

Additional comments

When the alternative_mailconfig of the com_mails component is set to 1 (“Per Template Mail Settings” is set to Yes) the Joomla\CMS\Mail\MailTemplate::send() method ALWAYS tries to use the mail template's mailer settings even if you have set the mail template's “Mail Settings” option to No. This happens because the only condition check is

if ($config->get('alternative_mailconfig'))

This problem has existed since the Mail Templates component was first introduced in Joomla 4.0 on September 28th, 2019 with PR #22126. In fact, the alternative_mailconfig option in the mail template itself has never worked at all ever since the feature was contributed.

There should really be TWO checks there. Off the top of my head:

if ($config->get('alternative_mailconfig') && (int) ($params->get('alternative_mailconfig', 0) === 1)

Further to that, the administrator/components/com_mails/forms/template.xml file should have a default value of 0 (not 1!) for alternative_mailconfig.

Moreover, the rest of the code in MailTemplate::send dealing with the custom mailer settings is wrong. For example, $smtpuser = $params->get('smtpuser', $app->get('smtpuser')); will always return the email template's setting for smtpuser even if that's left empty. Defaulting to the application's global configuration parameter will never happen since Joomla never does store NULL for empty string fields, it stores an empty string which does NOT trigger the Registry's default return in get(). That's why saving a mail template with no mailer configuration currently doesn't use the Global Configuration and how I ultimately found out about this bug.

If someone else could confirm the issue I will make a PR against 4.1 addressing all three issues.

BTW, is any other 3PD using this feature or am I the only one...? It beggars belief that I am the first one to notice this fairly obvious problem after three years of that code being in the core. Huh.

avatar nikosdion nikosdion - open - 30 May 2022
avatar joomla-cms-bot joomla-cms-bot - change - 30 May 2022
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 30 May 2022
avatar Kostelano
Kostelano - comment - 31 May 2022

I can confirm that according to the instructions we get an error. True, I got another error without making changes to php.ini.

SMTP Error: Could not connect to SMTP host.

avatar nikosdion
nikosdion - comment - 31 May 2022

Thank you for the confirmation! Now there are three of us who experienced it I am calling it a bug. I will come back with a PR shortly. Once again, thank you!

avatar nikosdion
nikosdion - comment - 31 May 2022

Opened pull request #37948.

@Kostelano Could you please give it a quick test?

avatar richard67 richard67 - change - 31 May 2022
Status New Closed
Closed_Date 0000-00-00 00:00:00 2022-05-31 20:56:42
Closed_By richard67
avatar richard67 richard67 - close - 31 May 2022
avatar richard67
richard67 - comment - 31 May 2022

Closing as having a pull request.

Add a Comment

Login with GitHub to post a comment