首页 > 解决方案 > 连接失败。错误 #2:stream_socket_client():无法连接到 ssl://smtp.gmail.com:465(连接被拒绝)

问题描述

我正在尝试使用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");
}

标签: phpgmailphpmailer

解决方案


你有没有尝试使用:

$mail->SMTPSecure = 'tls';

and/or :

$mail->Port = 587;

推荐阅读