1

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?

6
  • Is this happening regardless of the values that are passed in or is the error dependent on variables holding specific values? Have you dumped your $_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. Commented Feb 9, 2022 at 23:27
  • How to catch SMTP connection errors? (from Toivo in 2020) and you might want to check if the native PHP 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. Commented Feb 9, 2022 at 23:37
  • @mickmackusa while debugging I found out that the culprit is my host, what do I do with this question? Do I just delete it? Commented Feb 10, 2022 at 14:16
  • I am trying very hard to grow this community. We get more visitors and contributors when we have more content. Deleting your page removes content. If you are able to provide a coherent/informative answer to your own question, please post a self-answer (and give it the green tick when the system allows you). On the other hand, if you are not able to post an educational answer, then I'd rather not see an abandoned page -- in which case you can delete your question. Increasing your rep and gaining privileges requires you to have posts, so there is also that incentive. Commented Feb 10, 2022 at 14:56
  • I'm a regular user, but I'm not using that account (or any account) while at work so... no privileges for me, but it's fine. Commented Feb 10, 2022 at 16:47

1 Answer 1

2

I found the answer to this question myself, but I have to thank mickmackusa for pointing me in the right direction by linking several debug instructions.

Since other people might encounter my same problem, this answer is directed at them.


Make sure your Sender uses the same domain as your site

Noticing how SMTPDebug also gives the "Could not instantiate mail server" error, I searched for that string in the PHPmailer GitHub wiki.

Since SiteGround makes it hard to change those settings, I logged to their support service to ask them how to do it, and while their tutorials about e-mails have been unhelpful, their "contact us - choose a category" tree has a "The website can't send e-mails" article in which there was a message for me, stating that they blocked my e-mails starting on 02/02/2022 because they have a different domain than the site and that could put me in trouble with spam detection.

SiteGround probably sent out a warning by e-mail, but as I'm working as a Collaborator (a SiteGround feature that lets users work on sites whose server is owned by someone else) I never got notified.

The solution was just to create e-mails within the SiteGround site management interface (a Collaborator can't do it, but the owner of the site can) and then use that as the Sender.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.