1

I'm sending email using the following code:

$mail =& JFactory::getMailer();
$email_sent = $mail->sendMail( 
        $from_email,
        $from_name,
        $to_email,
        $subject,
        $email_body,
        $sendas,
        $cc_emails,
        $bcc_emails,
        $email_attachments,
        $reply_to_email,
        $reply_to_name );
if($email_sent) {
    echo "Mail sent.";
} else {
    echo "Mail NOT sent.";
}

All of the variables are set to the appropriate values.

When $email_sent is false, is there a way to capture the error message or another way to find out why the email wasn't sent?

I'm on Joomla 3.7 and php 5.6.

2

Firstly, just on a side note, please remove the & before JFactory

There was a pull request submitted on Github a year ago that allowed Joomla to catch the errors being throw by PHPMailer:

https://github.com/joomla/joomla-cms/pull/9881

I'm not sure at the implementation but according to the documentation, the send() function will return a JError object if any errors occurred:

https://docs.joomla.org/Sending_email_from_extensions

  • Thanks for the info. Seems like everything I could find was for previous versions of Joomla. I switched to send from sendMail (and removed the "&"), and it seems to work now. – Sloan Thrasher Jun 14 '17 at 5:09
0

None of the proposed solutions work. The jMailer return value is a headache because you can't catch exceptions or even know the type of the send() return.

If success returns a boolean true; if not it returns a Jexception object, the two outcomes loosely evaluate as boolean true values!

The simplest way I found is:

$result = $mail->Send();
if ($result === true)  return true;
else  return false;
  • The second link in the accepted answer (to the Joomla docs) shows how to perform a strict check on the result with $send = $mailer->Send(); if ( $send !== true ) { This is just reversing the design of the condition block that you have posted, but effectively the same logic is already provided by the Joomla -- very clearly too. You must have misunderstood the implementation when you tried. – mickmackusa Feb 22 at 21:29
-1

You can get the error using a conditional statement, like so:

if (!$send)
{
    $error = $send->__toString();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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