skip to Main Content

Looking for help with my PHPMailer code if possible please.

Overall, it works fine, what I’m struggling to capture is the specific error messages.

if (!$mail->send()) {
 echo "Mail <font color=red><b>Not</font></b> Sent to $address - Error: $mail>ErrorInfo<br>";
 }
else {
 echo "Mail <font color=green><b>Successfully</font></b> Sent to $address<br>";
 }

This works okay, but the ErrorInfo I receive for an Invalid address for example is:You must provide at least one recipient email address

Whereas if I look in the actual debug log, I can see an actual error:
Invalid address: (bcc): steven.rothera.com

Can anyone help me get it so I can echo the actual error message please?

I’ve looked at a bunch of other posts to try help me get to the bottom of this but no luck so far.

To cover off some of the code that is present:

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
$mail = new PHPMailer(true);
$mail->SMTPDebug = 2;
$mail->Debugoutput = function($str, $level) {
  $fp = fopen('/var/log/phpmailer.log','a');
  fwrite($fp, date('d-m-Y H:i:s') . "t" . $str);
  fclose($fp);
};

I have tried to use exceptions also, but this didn’t show me any errors:

try {
 $mail->send();
  echo 'Message sent!';
      } catch (Exception $e) {
          echo $e->errorMessage(); //Pretty error messages from PHPMailer
      } catch (Exception $e) { 
        echo $e->getMessage(); //Boring error messages from anything else!
      }

3

Answers


  1. Here’s an updated version of your code with some modifications and additional suggestions for debugging:

    use PHPMailerPHPMailerPHPMailer;
    use PHPMailerPHPMailerSMTP;
    use PHPMailerPHPMailerException;
    
    $mail = new PHPMailer(true);
    
    try {
        // your code to configure PHPMailer
        // Attempt to send the email
        $mail->send();
        echo 'Message sent!';
    } catch (Exception $e) {
        // PHPMailer specific exception
        echo 'Mailer Error: ' . $e->getMessage();
    } catch (Exception $e) {
        // Generic exception
        echo 'An error occurred: ' . $e->getMessage();
    }
    
    // Additional Debugging Information
    echo 'Debug Output:<br>';
    echo nl2br(file_get_contents('/var/log/phpmailer.log')); 
    

    Make sure to do the following:

    • File Permissions
    • Check SMTP Conntections
    Login or Signup to reply.
  2. $mail->ErrorInfo You can get the error message, try it

      $mail = new PHPMailer();
       // use smtp service
       $mail->isSMTP();
       // send html
       $mail->isHTML();
       // The encoding format is UTF 8. If the encoding is not set, Chinese characters will appear garbled.
       $mail->CharSet = "utf8";
       // sender s smtp server address
       $mail->Host = $host;
       // whether to use authentication
       $mail->SMTPAuth = true;
       // The sender’s email username is the email address used by your own SMTP service.
       $mail->Username = $username;
       // The sender’s email password. Note that the “client authorization password” is filled in here instead of the email login password!
       $mail->Password = $password;
       // use ssl protocol
       $mail->SMTPSecure = "ssl";
       // the ssl protocol port number is 465
       $mail->Port = 465;
       // Set the sender information, such as the sender in the email format description
       $mail->setFrom($username, $sender_name);
       // Set the recipient information, such as the recipient in the email format description
       $mail->addAddress($toemail, $sender_name);
       // Set the respondent information, which refers to the email address to which the reply email will be sent if the recipient wants to reply after receiving the email.
       $mail->addReplyTo($username, $sender_name);
       // mail title
       $mail->Subject = $subject;
       // email text
       $mail->Body = $body;
       if($mail->ErrorInfo) {
           // failed to send
          return ['code' => 0, 'msg' => $mail->ErrorInfo];
       }
       if(!$mail->send()) {
          // failed to send
          return ['code' => 0, 'msg' => $mail->ErrorInfo];
       }
       // sent successfully
       return ['code' => 1, 'msg' => ''];
    
    Login or Signup to reply.
  3. PHPMailer can only report the errors it knows about i.e. the errors which are returned via SMTP. If your code is not reflecting what the MTA is logging, then the first step would be to look at the SMTP exchange. If the MTA is writing important stuff to the log which it is not telling the client about then your only course of action is to change the MTA – not your code.

    Turn the debugging up to 4 and if this still does not answer your question show us the SMTP exchange

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search