Attention!
While searching for a solution I have found that the hosting service blocked mails, I'm not sure this is a valid question anymore, don't reply untile further notice.
The issue
Quoting 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.
Well, not anymore - and I have not touched anything:
- Last time I made any changes to the site (including Joomla updates) was in December.
- To my knowledge, Joomla! uses PHPmailer, whose last changelog entry is from November.
- The PHP file was still working properly 7 days ago (02/02/2022).
- The PHP file was first reported giving an error on 07/02/2022.
- My IT manager didn't recently introduce any firewall rules that might block e-mails from my site.
Testing and debugging
After some testing, I am positive that the ajax call works properly (it outputs the expected payload) and that the PHP receives it (I have temporarily modified the PHP to echo the querystring instead of trying to send the e-mail and it does so correctly).
The problem, therefore, lies somewhere in the php file:
//_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();
}
No matter the input (which is validated and encoded with encodeURIcomponent in the js), the try/catch structure catches the "Failed to start application" exception.
Commenting out $mail->Send();
and nothing else echoes "Mail sent", as expected (of course, the mail is not sent)
I think this means that the mailer is instantiated correctly, or it would give some exception as soon as I try adding a recipient, instead it only fails way later.
Note: from now on, echo results with this line commented out will be labeled "try echo" and echo results with the line causing an exception will be labeled "catch echo".
Following suggestions by mickmackusa (who linked me some resources in the comments) I tried debugging by adding the following just before the try/catch structure...
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
...and by echoing !$mail->isError();
in both the try and the catch, which gives me the following results:
- Try echo = 1
- Catch echo = nothing
Adding $mail->isHtml(true);
changes nothing at all.
Adding $mail->SMTPDebug = 2;
(or 3) gives the following results:
- Catch echo = Could not istantiate mail function.
It looks like the PHP installation is not configured to call the mail()
function correctly.
I have tried seeing if other people in forums had my same problems but all I found was the idea to check the mail sending parameters in Joomla!'s Global Configuration page.
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: [email protected] Sender name: Firefighter's company's page Reply to e-mail: Reply to name: E-mail function: PHP mail
Notably, sending a test e-mail fails with the following message:
Could not instantiate mail function.
The same as SMTPDebug.
The question
How do I make my mailer work again?
$_REQUEST
array to see what you are receiving? Do we need to review this data for possible problems? I think if I was going to create a form that sends an email (performs an action that does more than simply Read data from the server), I'd use$_POST
. (I never use$_REQUEST
in any professional application) Checking the Joomla Forum for advice on debugging the mailer will dredge up very old posts, but may inspire.mail()
function works. And you might try @Harald's advice Do you want to check if$from
is a valid email? There could be a wide array of problems e.g. startnet.co.uk/… Please see if you can dig up some additional debugging clues.