2

I'm using the PHPMailer Class to send the smtp mails, but i need to trace log from PHPmailerException in mysql table,

Code :

 $mail = new PHPMailer(true);
    try {
    $mail->Host =$dmmodel->host;
    $mail->Username= $dmmodel->username;
    $mail->Password= $dmmodel->password;
    $mail->Mailer='smtp';
    $mail->Port=$dmmodel->port;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = ($dmmodel->smtp_enableSSL==1?'ssl':($dmmodel->smtp_enableSSL==2 ? 'tls':''));
    $mail->SMTPDebug = 2;
    $mail->From = $dmmodel->email_from;
    $mail->FromName = $dmmodel->name_from;
    $mail->Subject    = $this->getContentBody($docmodel->content_subject,$docmodel->crm_base_contact_id,$_POST["taskid"],$postcode,$recall_by,$recall_dt,true);
    $mail->AddAddress($email[$i]);
    $mail->IsHTML(true); 
}
   catch (phpmailerException $e)
    {
         $msg = "Email Delivery failed -" .  $e->errorMessage();
         echo "Email not sent";
    } 

Where $msg variable has only message "SMTP Error: Could not authenticate" but when i put alert , i am getting full message(as below) reason for this issue,

SMTP -> FROM SERVER:220 smtp.gmail.com ESMTP n3sm27565307paf.13 - gsmtp
SMTP -> FROM SERVER: 250-smtp.gmail.com at your service, [122.164.189.101] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
SMTP -> ERROR: Password not accepted from server: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials n3sm27565307paf.13 - gsmtp
SMTP -> FROM SERVER:250 2.1.5 Flushed n3sm27565307paf.13 - gsmtp
Email not sent

how can i store the above message in a variable ? Please can anyone help me to get this full message

2

2 Answers 2

8

Can you try:

this one:

$mail->SMTPDebug = 4;

instead of

$mail->SMTPDebug = 2;

Also, you can get more info about the error with the method

$mail->ErrorInfo.

For example:

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

This is an alternative to the exception model that you need to active with new PHPMailer(true).

5

After setting $mail->SMTPDebug = 2;, you need to create a callback function and assign it to $mail->Debugoutput. In that function, you can assign the debug output to a variable. Here's the documentation on this subject: http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#property_Debugoutput

Please note: Your callback function will be called once per line of debug output (as opposed to just once with all of the debug output in the error string), so you'll have to append each line to your variable. If you just assign it, you'll only get the last line of debug output, which is often the same information that's in $mail->ErrorInfo or the exception.

I prefer to do something like this:

$GLOBALS['debugOutput'] = [];

$mail->Debugoutput = function($debugOutputLine, $level) {
    $GLOBALS['debugOutput'][] = $debugOutputLine;
};

//...Put your mail code here that could cause an error

$debug_output = implode("\n", $GLOBALS['debugOutput']);
echo $debugOutput;

This should print out the same information as in your example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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