【问题标题】:Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)连接失败。错误 #2:stream_socket_client():无法连接到 ssl://smtp.gmail.com:465(连接被拒绝)
【发布时间】:2018-10-23 11:07:46
【问题描述】:

我正在尝试使用PHPMailer,但出现以下错误:

连接失败。错误 #2:stream_socket_client(): 无法连接到 ssl://smtp.gmail.com:465(连接被拒绝)

这是我的代码:

$mail = new PHPMailer;
$mail->SMTPDebug = 4;                               // Enable verbose debug  output
$mail->isSMTP();                                      // Set mailer to use SMTP  
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication  
$mail->Username = 'test@gmail.com';                 // SMTP username
$mail->Password = 'some_password';                           // SMTP password  
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted  
$mail->Port = 465;                                    // TCP port to connect to  
$mail->setFrom('earndreamsol@gmail.com' );
$mail->addAddress($email);     // Add a recipient      
//$mail->addReplyTo($email, $name);  
$mail->isHTML(true);                                  // Set email format to HTML  
$mail->Subject = "Email from Alahmed & Co";  
$mail->Body    = " Thanks For Subscribe Alahamed & co"; 
$mail->AltBody = "Thanks For Subscribe Alahamed & co";
if(!$mail->send()) {  
    echo 'Message could not be sent.';  
    echo 'Mailer Error: ' . $mail->ErrorInfo;  
} else {  
    $_SESSION['msg']="you are successfully subscribed !!";  
    $_SESSION['msg1']="you are successfully subscribed !!";
    header("location:index.php#newssection");
}

【问题讨论】:

  • 在您的 gmail 帐户中,您必须允许从 less secure methods 发送邮件(google 的术语,不是我的)。
  • 如果那是你的密码,那么它应该被删除。要求版主删除历史记录。
  • 您的 ISP 可能会阻止出站 SMTP - 请阅读故障排除指南以进行诊断。看起来您可能正在使用旧版本的 PHPMailer,并且您的代码基于过时的示例,因此请确保您是最新的。

标签: php gmail phpmailer


【解决方案1】:

你有没有尝试使用:

$mail->SMTPSecure = 'tls';

and/or :

$mail->Port = 587;

【讨论】:

  • 连接失败。错误 #2: stream_socket_client(): 无法连接到 smtp.gmail.com:587 (Connection denied) 这种错误我得到了
  • 同样的错误ErrorException stream_socket_enable_crypto(): Peer certificate CN=sgvip1.noc401.com' did not match expected CN=smtp.gmail.com'
猜你喜欢
  • 2020-12-31
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2017-03-13
  • 2020-03-25
  • 2017-03-08
相关资源
最近更新 更多