Search code examples
phpphpmailer

Save PHPMailer debug to database not working


I have a php script that sends emails using PHPMailer see below:

$mail = new PHPMailer(true);
try {
    $debug = NULL;
    $mail->Debugoutput = function($str, $level) {
        $GLOBALS['debug'] .= "$level: $str\n";
    };
    $mail->isSMTP();
    $mail->Host = 'xxxxxxx';
    $mail->SMTPDebug = 2;
    $mail->SMTPAuth = true;
    $mail->Username = 'xxxxxx';
    $mail->Password = 'xxxxxx';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    $mail->setFrom('[email protected]', 'xxx');
    $mail->addAddress('[email protected]');
    $mail->isHTML(true);
    $mail->Subject = 'xx';
    $email_content = '<p>test</p>';
    $mail->Body = $email_content;
    $mail->send();
    fncSaveLog($debug);
} catch (Exception $e) {
    fncSaveLog($mail->ErrorInfo);
}

the fncSaveLog is a function that saves debug info to database but I see always NULL for debug in my DB, it seems the output is never caught. Any idea how to fix this please?

Thanks.


Solution

  • Your setting NULL to save into the db. Try the below instead:

    $mail = new PHPMailer(true);
    try {
    
        $mail->Debugoutput = function($str, $level) {
            $GLOBALS['debug'] .= "$level: $str\n";
        };
        $mail->isSMTP();
        $mail->Host = 'xxxxxxx';
        $mail->SMTPDebug = 2;
        $mail->SMTPAuth = true;
        $mail->Username = 'xxxxxx';
        $mail->Password = 'xxxxxx';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
        $mail->setFrom('[email protected]', 'xxx');
        $mail->addAddress('[email protected]');
        $mail->isHTML(true);
        $mail->Subject = 'xx';
        $email_content = '<p>test</p>';
        $mail->Body = $email_content;
        $mail->send();
        $debug = $GLOBALS['debug'];
        fncSaveLog($debug);
    } catch (Exception $e) {
        fncSaveLog($mail->ErrorInfo);
    }