The issue
Quoting (again) from an old question of mine:
I have a form in my Joomla! site. Once completed, the form should send an e-mail but only if some conditions have been met.
Not having been able to use standard, commercially available mailers while also checking those conditions, I am using jQuery.ajax to POST the content of the form to a custom PHP page that uses Factory::getMailer to actually send the e-mail.
Maybe it's convoluted, but it works.
After updating the site from Joomla3 to Joomla4, it stopped working.
The code is the exact same as in this question I posted a while ago:
//_JEXEC must be 1 or require_once requests will fail because of their "_JEXEC or die"
define('_JEXEC', 1);
//Then I need to require some Joomla! files where getMailer is defined
define('JPATH_BASE', __DIR__);
require_once (JPATH_BASE.'/includes/defines.php');
require_once ('includes/framework.php');
try {
//fetching variables from querystring
$name=$_POST['name'];
$body=$_POST['body'];
$from=$_POST['from'];
//invoke mailer
$mail = \Joomla\CMS\Factory::getMailer();
$mail->setSender('[email protected]');
$mail->addRecipient('[email protected]');
$mail->setFrom($from,$name); # who is it?
$mail->addReplyTo($from,$name); # who do I reply to?
$mail->setSubject('E-mail from Firefighter\'s form');
$mail->setBody($body);
$mail->ClearCCs();
$mail->ClearBCCs();
$mail->ClearAttachments();
$mail->ClearCustomHeaders();
$mail->Send();
echo "Mail sent";
exit;
}
catch (Exception $e) {
echo $e->getMessage();
}
Some considerations:
- I restored a Joomla3 backup and it works, so I don't think it is a problem with PHPmailer versions
- I have checked with the hosting and they are not blocking any e-mails this time (last time, they were the cause of the problem)
Testing and debugging
- I have had the host activate php debugging and it doesn't log anything.
- I have activated Joomla4's maximum level of error reports and nothing shows up.
- I have placed an echo command right before Send(), and I get that right before the "failed to start application" from the catch echo.
- Commenting out
$mail->Send();
and nothing else echoes "Mail sent", as expected (of course, the mail is not sent). - Joomla's test e-mail works correctly.
So, everything works properly but the Send() function
I have not had time yet to check the following:
Just before the catch structure:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Adding $mail->isHtml(true);
Adding $mail->SMTPDebug = 2;
(or 3)
I don't know if it's related to my problem (after all, I have not changed those settings in the last few weeks) but here are the settings, translated back from my language:
E-mail sending: yes Disable user e-mail sending: no Email from: [the mail I use for ftp access] Sender name: Firefighter's company's page Reply to e-mail: Reply to name: E-mail function: PHP mail
The question
How do I make my mailer work again?