php - 连接失败。错误 #2 : stream_socket_client(): unable to connect to ssl://smtp. gmail.com:465(连接被拒绝)

标签 php gmail phpmailer

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

Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)

这是我的代码:

$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");
}

最佳答案

你有没有尝试使用:

$mail->SMTPSecure = 'tls';

and/or :

$mail->Port = 587;

关于php - 连接失败。错误 #2 : stream_socket_client(): unable to connect to ssl://smtp. gmail.com:465(连接被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52947507/

相关文章:

php - 访问不同服务器上的数据库,无需使用主主/主从复制

email - 使用 msmtp 通过终端发送邮件工作正常,但不适用于 php mail()

c++ - 在电子邮件地址内移动点(句点)的算法

PHPMailer 生成 PHP 警告 : stream_socket_enable_crypto(): Peer certificate did not match expected

PhpMailer SMTP 通知 : EOF caught while checking if connected

php - 如何获取两个或多个表中的同一列?

php - 分离 Web 应用程序各部分的经验法则

php - Ajax jQuery 数组中的 POST 未定义索引

Python套接字突然超时?

PHPMailer 异常 :SendAsDeniedException. MapiExceptionSendAsDenied