Phpmailer debug output
PHPMailer provides several ways to debug and troubleshoot issues with your email sending code. Here are a few methods to output debug information:
-
Enable debug output: You can enable debug output by setting the
Debugoutput
property to a function that will output the debug information. For example:$mail->Debugoutput = function($str, $level) { echo "<p>Debug: $str</p>"; };
This will output the debug information as HTML paragraphs.
-
Set the
SMTPDebug
option: You can set theSMTPDebug
option to a value between 1 and 3 to control the level of debug output. For example:$mail->SMTPDebug = 2;
This will output more detailed debug information, including the SMTP conversation.
-
Use the
ErrorInfo
property: You can access the error information using theErrorInfo
property. For example:echo $mail->ErrorInfo;
This will output the last error message.
-
Use the
PrintDebug
method: You can use thePrintDebug
method to output the debug information. For example:$mail->PrintDebug();
This will output the debug information as a string.
Here's an example of how you can use these methods to output debug information:
<?php
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->Debugoutput = function($str, $level) {
echo "<p>Debug: $str</p>";
};
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test email';
$mail->Body = 'This is a test email';
if (!$mail->send()) {
echo "Error: ". $mail->ErrorInfo;
} else {
echo "Email sent successfully";
}
$mail->PrintDebug();
?>
This code will output the debug information as HTML paragraphs, and also print the error information if the email sending fails.