Skip to main content
formatting
Source Link

###The question

The question

###The question

The question

included more information after debugging
Source Link

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:

Well, not anymore. - and I have not touched anything:

  • Last time I touchedmade any changes to the site (including Joomla updates) was in December.
  • The last changes to the mailer (which isTo my knowledge, Joomla! uses PHPmailer, right?) were inwhose last changelog entry is from November.
  • The PHP file was still working properly 7 days ago (02/02/2022).
  • The PHP file returns a "Failed to start application" message since yesterday (and it does not sendwas first reported giving an error on 07/02/2022.
  • My IT manager didn't recently introduce any firewall rules that might block e-mail)mails from my site.

I think this means that the try/catch structure of the PHP caught an exception with that name.

I have no idea why this happens and, after having scoured the forums for the large part of this working day, it's time to ask someone more knowledgeable.

Testing and debugging

After some testing, I have checkedam positive that the ajax call's payload in Chrome's DevToolscall works properly (Network > request > Payloadit outputs the expected payload) and it's still passingthat the correctPHP receives it (I have temporarily modified the PHP to echo the querystring instead of trying to my PHP mailersend the e-mail and it does so correctly).

This makes me think theThe problem is with, therefore, lies somewhere in the PHP. Here it is (anonymized to protect my Company)php file:

//_JEXEC must be 1 or require_once requests will fail because of their "_JEXEC or die"
define('_JEXEC', 1);

//Then #I Neededneed forto therequire 'require_once'some callsJoomla! below.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(); # Invoke JMail Class
    $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();
}

It happened to meNo matter the input (which is validated and encoded with encodeURIcomponent in the past thatjs), the mailer changed syntaxtry/catch structure catches the "Failed to start application" exception.

Commenting out $mail->Send(); and it broke my codenothing else echoes "Mail sent", but as I mentioned there's not been an update inexpected (of course, the last few weeks somail is not sent)

I don't 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 reasoncomments) 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);

My IT manager didn't recently introduce any firewall rules that might block e-mails from my site...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.

E-mail sending: yes Disable user e-mail sending: no Email from: example.exampleexample@example.com Sender name: Firefighter's company's page Reply to e-mail: Reply to name: E-mail function: PHP mail

Anyway, The same as SMTPDebug.

###The question

howHow do I make my mailer work again?

(Of course, requests for more informations or instructions on how to gather more specific information on the error are welcome, as long as I don't need to disclose details about my company.)

Quoting from an old question of mine:

Well, not anymore.

  • Last time I touched the site (including Joomla updates) was in December
  • The last changes to the mailer (which is PHPmailer, right?) were in November
  • The PHP file was still working properly 7 days ago (02/02/2022)
  • The PHP file returns a "Failed to start application" message since yesterday (and it does not send any e-mail).

I think this means that the try/catch structure of the PHP caught an exception with that name.

I have no idea why this happens and, after having scoured the forums for the large part of this working day, it's time to ask someone more knowledgeable.

I have checked the ajax call's payload in Chrome's DevTools (Network > request > Payload) and it's still passing the correct querystring to my PHP mailer.

This makes me think the problem is with the PHP. Here it is (anonymized to protect my Company):

define('_JEXEC', 1); # Needed for the 'require_once' calls below.
define('JPATH_BASE', __DIR__);
require_once (JPATH_BASE.'/includes/defines.php');
require_once ('includes/framework.php');
try {
    $name=$_POST['name'];
    $body=$_POST['body'];
    $from=$_POST['from'];
    $mail = \Joomla\CMS\Factory::getMailer(); # Invoke JMail Class
    $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();
}

It happened to me in the past that the mailer changed syntax and it broke my code, but as I mentioned there's not been an update in the last few weeks so I don't think this is the reason.

My IT manager didn't recently introduce any firewall rules that might block e-mails from my site.

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.

E-mail sending: yes Disable user e-mail sending: no Email from: example.example.com Sender name: Firefighter's company's page Reply to e-mail: Reply to name: E-mail function: PHP mail

Anyway, how do I make my mailer work again?

(Of course, requests for more informations or instructions on how to gather more specific information on the error are welcome, as long as I don't need to disclose details about my company.)

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:

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.

E-mail sending: yes Disable user e-mail sending: no Email from: example@example.com Sender name: Firefighter's company's page Reply to e-mail: Reply to name: E-mail function: PHP mail

The same as SMTPDebug.

###The question

How do I make my mailer work again?

POST instead of REQUEST, as suggested by @mickmakusa
Source Link
define('_JEXEC', 1); # Needed for the 'require_once' calls below.
define('JPATH_BASE', __DIR__);
require_once (JPATH_BASE.'/includes/defines.php');
require_once ('includes/framework.php');
try {
    $name=$_REQUEST['name'];$name=$_POST['name'];
    $body=$_REQUEST['body'];$body=$_POST['body'];
    $from=$_REQUEST['from'];$from=$_POST['from'];
    $mail = \Joomla\CMS\Factory::getMailer(); # Invoke JMail Class
    $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();
}
define('_JEXEC', 1); # Needed for the 'require_once' calls below.
define('JPATH_BASE', __DIR__);
require_once (JPATH_BASE.'/includes/defines.php');
require_once ('includes/framework.php');
try {
    $name=$_REQUEST['name'];
    $body=$_REQUEST['body'];
    $from=$_REQUEST['from'];
    $mail = \Joomla\CMS\Factory::getMailer(); # Invoke JMail Class
    $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();
}
define('_JEXEC', 1); # Needed for the 'require_once' calls below.
define('JPATH_BASE', __DIR__);
require_once (JPATH_BASE.'/includes/defines.php');
require_once ('includes/framework.php');
try {
    $name=$_POST['name'];
    $body=$_POST['body'];
    $from=$_POST['from'];
    $mail = \Joomla\CMS\Factory::getMailer(); # Invoke JMail Class
    $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();
}
Source Link
Loading