Cannot handle PHPMailer errors.

Created on 7 December 2015, about 9 years ago
Updated 31 May 2023, over 1 year ago

To handle PHPMailer errors by Exceptions, PHPMailer object must be instantiated with parameter. I found it on http://stackoverflow.com/questions/2386544/error-handling-with-phpmailer

smtp.mail.inc dont use it in mail() function, so errors are handled by 'echo' so i get nasty message before page html is created

    // Create a new PHPMailer object - autoloaded from registry.
    $mailer = new PHPMailer();

it should be:

    // Create a new PHPMailer object - autoloaded from registry.
    $mailer = new PHPMailer(true);

this is how drupal page source looks like without proper error handling:

Message body empty
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
  "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" version="XHTML+RDFa 1.0" dir="ltr">

I want my code to look like this:

try {
        $MailSystem = drupal_mail_system('smtp', 'xxx');
        $message2 = $MailSystem->format($message);
        $result = $MailSystem->mail($message2);

        if($result) {
            return array('status'=>TRUE, 'message'=>$message);
        } else {
            return array('status'=>FALSE, 'message'=>"My custom error message");
        }

    }
    catch(Exception $e) {
        return array('status'=>FALSE, 'message'=>"My custom error message");
    }
🐛 Bug report
Status

Closed: outdated

Version

1.2

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024