SMTP GMAIL Connection


SMTP GMAIL Connection

我无法使用PHPMailer连接到SMTP GMAIL。

这里有错误:

2015年6月25日22:54PM出现错误-stream_socket_client():无法在中连接到smtp.gmail.com:587(连接超时)/home/amiroper/public_html/beporsbedoon/app/helpers/phpmailer/smtp.php在线222

这是我的代码:

$this->_mail->isSMTP();
$this->_mail->Host = "smtp.gmail.com";
$this->_mail->SMTPAuth = true;
$this->_mail->Username = "amiroperator@gmail.com";
$this->_mail->Password = "*********";
$this->_mail->SMTPSecure = "tls";
$this->_mail->Port = "587";
$this->_mail->SMTPDebug = 4;
$this->_mail->From = "AmirOperator";
$this->_mail->FromName = 'amiroperator@gmail.com';
$this->_mail->addAddress("amiroperator@outlook.com", "test");
$this->_mail->isHTML(true);
$this->_mail->Subject = 'Registration confirm';
$this->_mail->Body    = 'Thank you for registering to activate your account please click on this link. ".DIR."account/activate/$id/$activasion"';
$this->_mail->AltBody = 'Thank you for registering to activate your account please click on this link. ".DIR."account/activate/$id/$activasion"';
if(!$this->_mail->send()) {
    $data['mailsent'] = false;
} else {
    $data['mailsent'] = true;
}

php代码错误或这是连接问题

调整代码中的身份验证协议和端口号:

// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Updated Settings
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 465;  

我发现,当PHPMailer设置为使用具有TLS身份验证协议和端口号587的Gmail SMTP服务器时,它根本不起作用。然而,我在使用SSL/465时从未遇到过问题。

谷歌SMTP设置

TLS和SSL 之间的差异

您的服务器无法连接到端口587上的smtp.gmail.com。我在一个测试工具上遇到了同样的问题:

Resolving hostname...
Connecting...
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
Message sending failed.

从我的本地机器上看,它工作得很好:

Trying 74.125.195.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP be3sm8900765wib.21 - gsmtp

也许谷歌在美国(我在德国)或类似的地方出现了暂时的问题。您的代码中没有明显的错误。您只能稍后重试,或者使用其他SMTP服务器重试。

此外,您可以尝试直接连接74.125.195.108(只需停用SSL证书验证)。


编辑:也可以尝试tls://smtp.gmail.com作为主机。