【问题标题】:Fatal error: Uncaught phpmailerException: SMTP connect() failed致命错误:未捕获的 phpmailerException:SMTP 连接()失败
【发布时间】:2021-02-12 02:18:22
【问题描述】:

我在发送email 时遇到以下错误

2020-10-29 15:34:02 连接:打开到 localhost:587,超时=300, options=array () 2020-10-29 15:34:04 连接失败。错误 #2: stream_socket_client(): 无法连接到 localhost:587 (否 可以建立连接,因为目标机器主动拒绝 它。)[C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.smtp.php 第 298 行] 2020-10-29 15:34:04 SMTP 错误:无法连接到服务器: 由于目标机器主动,无法建立连接 拒绝了。 (10061) SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

致命错误:未捕获的 phpmailerException:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 在 C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php:1558 堆栈跟踪:#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} 抛出 C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php 上 第 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(); 
}
?>

我不知道我哪里错了

【问题讨论】:

    标签: php email smtp phpmailer


    【解决方案1】:

    几个简单的错误。

    首先,您使用的是非常旧的 PHPMailer 版本。 Upgrade,最好改用作曲家。

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

    接下来有点概念性 - 您无法为localhost 获得有效证书(由公共 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();
    

    【讨论】:

    • 我将主机更改为smtp.gmail.com,将密码更改为secret(我不知道要添加什么密码),但出现以下错误,Fatal error: Uncaught phpmailerException: SMTP Error: Could not authenticate.
    • 阅读调试输出的其余部分和 PHPMailer 故障排除指南,这很可能涵盖 gmail 的任何身份验证问题。
    猜你喜欢
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2020-01-31
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多