【问题标题】:SMTP GMAIL ConnectionSMTP GMAIL 连接
【发布时间】:2015-09-12 12:20:02
【问题描述】:

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

这里是错误:

2015 年 6 月 25 日下午 22:54 出错 - 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代码错误或者是连接问题

【问题讨论】:

  • 这是一个连接问题 - 您的 DNS 不工作。

标签: php smtp phpmailer


【解决方案1】:

您的服务器无法连接到端口 587 上的 smtp.gmail.com。我在a testing tool 遇到了同样的问题:

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 作为主机。

【讨论】:

  • 谢谢,我来自伊朗。可能在伊朗,我们在 smtp.gmail.com 上遇到了暂时的问题,但我可以使用 74.125.195.108 进行连接。
  • 这意味着您的 DNS 有问题。
  • @Synchro:我不这么认为,因为连测试页都失败了。也许这是 Google 对 SSL/TLS 支持的问题。测试工具明确表示,smtp.gmail.com 已解决。
  • @amirooperator:请注意修复 IP 地址,因为它可以更改或进行负载平衡。您是否也尝试过使用 tls:// 前缀和主机名?
  • 您可以使用前缀或 SMTPSecure 属性,使用哪个没有区别。
【解决方案2】:

在你的代码中调整认证协议和端口号:

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

// Updated Settings
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 465;  

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

Google SMTP Settings

Difference Between TLS and SSL

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多