Fatal error: Uncaught phpmailerException: SMTP connect() failed

Fatal error: Uncaught phpmailerException: SMTP connect() failed

发送 email

时出现以下错误

2020-10-29 15:34:02 Connection: opening to localhost:587, timeout=300, options=array () 2020-10-29 15:34:04 Connection failed. Error #2: stream_socket_client(): unable to connect to localhost:587 (No connection could be made because the target machine actively refused it.) [C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.smtp.php line 298] 2020-10-29 15:34:04 SMTP ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it. (10061) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Fatal error: Uncaught phpmailerException: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php:1558 Stack trace: #0 C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php(1340): PHPMailer->smtpSend('Date: Thu, 29 O...', 'This is a multi...') #1 C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php(1215): PHPMailer->postSend() #2 C:\xampp\htdocs\Hospital\adminpanel\table\apt status.php(56): PHPMailer->send() #3 {main} thrown in C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php on line 1558

以下是我试过的php代码

<?php

include '../auth/dbconnection.php';
require ('../phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer(TRUE);

 //Server settings
 $mail->isSMTP();                             // Set mailer to use SMTP
 $mail->Host       = 'localhost';             // Set the SMTP server to send through
 $mail->SMTPAuth   = true;                    // Enable SMTP authentication
 $mail->SMTPDebug = 4; 
 $mail->Username   = 'seushms@gmail.com';     // SMTP false username
 $mail->Password   = '';                      // SMTP false password
 $mail->SMTPSecure = 'tsl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
 $mail->Port       = 587;                // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

 
 $stmt1 = $conn->prepare("SELECT * FROM patients WHERE p_id=?");
 $stmt1->bind_param("s", $_POST['p_id']);
 $stmt1->execute();
 $result = $stmt1->get_result();
  if(mysqli_num_rows($result)>0){
    while($row = $result->fetch_assoc()) {
     $username=$row['username'];
     $email=$row['email'];
     
       //Recipients
   $mail->setFrom('seus@gmail.com', 'Mailer');
   $mail->addAddress($email);     // Add a recipient
   $mail->addReplyTo('seus@gmail.com');

    }
}

if (isset($_POST['data_id'])) {

  $id=$_POST['data_id'];
  $date=$_POST['date'];

   $stmt = $conn->prepare("UPDATE appointment SET admin_status = ?,patient_status = ? WHERE apt_id= ?");
   $admin_status='1';
   $patient_status='1';

   $stmt->bind_param("ssi", $admin_status,$patient_status, $id);
   $status = $stmt->execute();

   if ($status === true) {

    // Content
    $mail->isHTML(true);     // Set email format to HTML
    $mail->Subject = "Regarding the Appointment Request";
    $mail->Body    = "<h3 align=right> Your Appointment requested on'.$date. 'has <b> approved.</b> <br> Please refer the patient schedule for time arrivals. <br> Best Regards from </b> <b><i>  SEUS Hospitals </i> </b>";

    $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    $mail->send();

    echo "Records was updated successfully."; 

} else {
    echo 'cant'; 
}

$conn->close(); 
}
?>

不知道哪里错了

几个直截了当的错误。

首先,您使用的是非常旧版本的 PHPMailer。 Upgrade,最好切换到使用 composer。

一个简单的错字:$mail->SMTPSecure = 'tsl'; 应该是 $mail->SMTPSecure = 'tls';

下一个更概念化 - 您无法获得 localhost 的有效证书(由 public CA 签名),因此您总是会遇到麻烦。无论如何,使用到 localhost 的加密连接没有任何好处,因为没有任何东西通过网络传输。提交到本地主机时也可能不需要身份验证。

我建议改变这一切:

 $mail->isSMTP();                             // Set mailer to use SMTP
 $mail->Host       = 'localhost';             // Set the SMTP server to send through
 $mail->SMTPAuth   = true;                    // Enable SMTP authentication
 $mail->SMTPDebug = 4; 
 $mail->Username   = 'seushms@gmail.com';     // SMTP false username
 $mail->Password   = '';                      // SMTP false password
 $mail->SMTPSecure = 'tsl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
 $mail->Port       = 587;                // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

仅此而已,因为其他一切都将使用默认值:

$mail->isSMTP();