简体   繁体   中英

phpmailer: how to debug only if recipient is accepted and email delivered

I have the usual phpmailer configuration as follows:

try {
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;  

    $mail->isSMTP();                                            
    $mail->Host       = $myhost;
    $mail->SMTPAuth   = true;                                   

    $mail->Username   = $myusername;                     
    $mail->Password   = $mypassword; 
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

    $mail->SMTPSecure = 'TLS';  
    $mail->Port       = 587;   

    $receiver = $receiver_arr[$r-1];
    $mail->setFrom($sender, $sender_full_denomination);
    $mail->addAddress($receiver, $receiver_full_denomination); 
    $mail->addReplyTo($sender, $replyto_full_denomination);


    $mail->isHTML(true);                                  
    $mail->Subject = $mail_subject;
    $mail->Body    = $mail_body;
    $mail->AltBody = $mail_body_alt;
    $mail->AddEmbeddedImage($file_path_1, $cid_1, $img_name_1);
    $mail->AddEmbeddedImage($file_path_2, $cid_2, $img_name_2);

    $mail->send();

} catch (Exception $e) {
    // ------ do some stuff with Mailer Error: {$mail->ErrorInfo}<br>";
}

Ii is perfectly working, but that verbose debug output is impossible to manage with hundreds of email to send, and nevertheless I need to check two things (and possibly store them into a variable so that I can record results in the db):

  1. if the recipient's email server is active;
  2. if the email is delivered to recipient's mailbox or if for any reason is bounced or refused (non existing recipient, mailbox full or whatever);

All that in procedural way, not OOP, since this is a committer decision.

How can I reach that?

You can choose exactly what you want to do with the debug output by injecting a callable into the Debugoutput property . For example:

$mail->Debugoutput = function($str, $level) {
    echo "debug level $level; message: $str";
};

You can do anything you like in there (meaning that it's entirely up to you), including resetting output between messages or recipients, logging to files, saving info in a database, etc.

No idea what you mean about being procedural; this is entirely OO, and has nothing to do with committers.

BTW, you're setting SMTPSecure twice. The first time is enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM