If PHPMailer fails, return to PHP's built-in mail function

The PHPMailer documentation in various examples of using the examples assumes that in the final part of the code - send - an error message should appear if it fails:

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

What I would prefer to do in the event of a failure is to return to the built-in PHP mail () function. The advantage is that the email will still be sent to me, and it may carry a PHPMailer failure notification.

// If PHPMailer code library fails (for whatever reason), fallback to mail()
if (!mail->send()) {

     //  just an example
     $to      = 'me@mydomain.com';
     $subject = 'You have a new e-mail subscriber + ERROR REPORT';
     $message = 'user input data and error description here';
     mail($to, $subject, $message);

} else {

     // other settings defined earlier in script
     $mail->Subject = 'You have a new e-mail subscriber';
     $mail->Body = $message;    
     $mail->send();

}

I hesitate to use the PHPMailer language as an expression IF, since a complete shutdown to PHP seems more secure and reliable for me. Is there a better way to write this?

: . PHPMailer mail(). . SMTP (, smtp.google.com), . , - , . , .

+4
2

PHPMailer. if !$mail->send() . else PHPMailer. . PHPMailer, , PHP.

 $mail->Subject = 'You have a new e-mail subscriber';
 $mail->Body = $message;    
 if (!$mail->send()) { 
     $to      = 'me@mydomain.com';
     $subject = 'You have a new e-mail subscriber + ERROR REPORT';
     $message = 'user input data and error description here';
     mail($to, $subject, $message);
 }
+2

, SMTP mail() PHPMailer:

...
$mail->isSMTP();
if (!$mail->send()) {
    //Fall back to using mail()
    $mail->isMail();
    if (!$mail->send()) {
        echo 'Message failed to send';
    } else {
        echo 'Message sent via mail()';
    }
} else {
    echo 'Message sent via SMTP';
}
+1

All Articles