Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full error message from PHPMailer Exception

Tags:

php

phpmailer

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

like image 305
US-1234 Avatar asked Jun 07 '26 05:06

US-1234


1 Answers

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).

like image 154
Hrvoje Antunović Avatar answered Jun 10 '26 09:06

Hrvoje Antunović