简体   繁体   English

如何解决:PHP中无法通过主机smtp.gmail.com建立连接[连接超时#110]错误

[英]How to resolve: Connection could not be established with host smtp.gmail.com [Connection timed out #110] error in PHP

I'm trying to set up an email verification system for a login system that I am working on. 我正在尝试为我正在使用的登录系统设置电子邮件验证系统。 I've got mostly everything working in my php code, up until the $mailer->send($message) line which returns the above error. 我大部分工作都在我的php代码中进行,直到$ mailer-> send($ message)行返回上述错误。

I've looked at several other similar issues on stackoverflow and other sites, but none of the proposed solutions have worked. 我已经在stackoverflow和其他站点上查看了其他几个类似的问题,但是所提出的解决方案都没有起作用。 I have less secure apps enabled on the gmail account that is sending the verification email. 我在发送验证电子邮件的gmail帐户上启用的安全性较低的应用程序。 I've tried ssl and tls with ports 465 and 587 respectively. 我已经尝试分别使用端口465和587的ssl和tls。 I don't wish to disable anything security related unnecessarily. 我不想禁用不必要的任何安全性。 While I am fairly confident in my coding knowledge, I'm afraid I have very little experience with server related issues like this. 尽管我对编码知识相当有信心,但恐怕我对类似服务器相关问题的经验很少。 I've created a login system with email verification successfully in the past, but I don't remember how I avoided this issue. 过去,我已经成功创建了一个通过电子邮件验证的登录系统,但是我不记得如何避免此问题。 If this is related to firewalls or something like that, I only ask for a little more information as one can assume I'm essentially a layman in that area. 如果这与防火墙或类似问题有关,我只要求提供更多信息,因为可以认为我本质上是该领域的外行。

<?php
require_once './vendor/autoload.php';

function sendVerificationEmail($userEmail, $verificationCode)
{
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
->setUsername("fromEmail@email.com")
->setPassword("password");

$mailer = new Swift_Mailer($transport);
$body = "";
    $message = (new Swift_Message('Verify your email'))
        ->setFrom("fromEmail@email.com")
        ->setTo($userEmail)
        ->setBody($body);

    echo "Message Created. Send To: " . $userEmail;
    try
    {
      if ($mailer->send($message, $errors))
      {
        echo "email success.";
        return true;
      }
      else
      {
        echo "email no good.";
        echo "ERROR: " . $errors;
        return false;
      }
    }
    catch(Swift_SwiftException $e)
    {
      echo "ERROR: " . $e->getMessage();
    }
}

?>

Much of this code is derived from the standard tutorials. 这些代码大部分来自标准教程。 I'm just trying to make sure an email is sent before I actually include the functionality for verification. 我只是想确保在实际包含验证功能之前先发送一封电子邮件。 I do not know why this is not sending an email. 我不知道为什么这不发送电子邮件。 It's specifically returning the 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' error. 它专门返回“无法与主机smtp.gmail.com建立连接[连接超时#110]”错误。

Thank you for your time. 感谢您的时间。

this is always a connection issue from your app to Google servers. 这始终是您的应用程序与Google服务器之间的连接问题。 Whenever Google rejects the connection, Google will actually return an error, including a KB article. 每当Google拒绝连接时,Google都会实际返回错误,包括知识库文章。 You can easily test the same connection using SMTP tester tools, or even any Outlook, phone IMAP/SMTP connection. 您可以使用SMTP测试器工具甚至任何Outlook,电话IMAP / SMTP连接轻松测试同一连接。 I'm afraid that I can't help you with the code, but I can surely tell you that the issue is not your Google account (I've seen this many times). 恐怕我无法为您提供代码,但是我可以肯定地告诉您问题不是您的Google帐户(我已经看过很多次了)。

You can also try Google's unrestrictive server (ASPMX.L.GOOGLE.COM) port 25, no auth, and it allows to test the connection without username/pw, but it only sends emails to google servers. 您也可以尝试使用Google的非限制性服务器(ASPMX.L.GOOGLE.COM)端口25(无身份验证),它可以测试不带用户名/密码的连接,但只能将电子邮件发送给Google服务器。

Good luck 祝好运

I eventually worked around the issue by using an SMTP plugin for Wordpress (WP Mail SMTP), where the page and login system are hosted. 我最终通过使用用于Wordpress的SMTP插件(WP Mail SMTP)解决了该问题,该插件托管了页面和登录系统。 It used some of the 0Auth2 stuff I set up, so I didn't completely waste my efforts. 它使用了我设置的一些0Auth2内容,所以我没有完全浪费我的精力。 Thank you all for your time and help. 谢谢大家的时间和帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift_TransportException无法与主机smtp.gmail.com建立连接[连接超时#110] - Swift_TransportException Connection could not be established with host smtp.gmail.com [Connection timed out #110] 无法在laravel 5.2中与主机smtp.gmail.com建立连接[连接超时#110] - Connection could not be established with host smtp.gmail.com [Connection timed out #110] in laravel 5.2 Swiftmailer:无法与主机 smtp.gmail.com 建立连接 [连接超时 #110] - Swiftmailer: Connection could not be established with host smtp.gmail.com [Connection timed out #110] Lravel邮件未发送-无法与主机smtp.gmail.com建立连接[连接超时#110] - Lravel Mail not sending — Connection could not be established with host smtp.gmail.com [Connection timed out #110] 未捕获的异常“Swift_TransportException”带有消息“无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]” - Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' 无法与主机smtp.gmail.com建立连接[操作超时#60] - Connection could not be established with host smtp.gmail.com [Operation timed out #60] 如何修复无法与 ZE1BFD762321E409CEEZ3AC06 中的主机 smtp.gmail.com 建立连接 - How to fix Connection could not be established with host smtp.gmail.com in php 无法与主机smtp.gmail.com [#0]建立连接 - Connection could not be established with host smtp.gmail.com [ #0] 无法与SwiftMailer中的主机smtp.gmail.com [#0]建立连接 - Connection could not be established with host smtp.gmail.com [#0] in SwiftMailer 无法与主机smtp.gmail.com建立连接 - Connection could not be established with host smtp.gmail.com Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM