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

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.

I've looked at several other similar issues on stackoverflow and other sites, but none of the proposed solutions have worked. I have less secure apps enabled on the gmail account that is sending the verification email. I've tried ssl and tls with ports 465 and 587 respectively. 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.

Thank you for your time.

dongsui8162
dongsui8162 我很确定我已经尝试了该线程中提到的所有内容。我也试过切换到phpMailer并使用谷歌的0Auth2设置,但也没有产生任何结果。
7 个月之前 回复
duande1985
duande1985 试着看到这个链接我希望这会有所帮助stackoverflow.com/questions/3478906/...
7 个月之前 回复
duanpu1111
duanpu1111 我不确定我完全理解,我道歉,我使用了smtp.gmail.com,smtp.office365.com,两者都使用ssl和tls以及端口465和587.如果你指的是像.env这样的东西文件,我不认为我有一个。如果不是上述之一,我可以问一下邮件配置是什么吗?谢谢你这么快回应。
7 个月之前 回复
douyaju4259
douyaju4259 请添加您目前使用或尝试过的邮件配置。
7 个月之前 回复

2个回答

I eventually worked around the issue by using an SMTP plugin for Wordpress (WP Mail SMTP), where the page and login system are hosted. It used some of the 0Auth2 stuff I set up, so I didn't completely waste my efforts. Thank you all for your time and help.

展开翻译

译文



我最终通过使用WordPress(WP Mail SMTP)的SMTP插件解决了这个问题,其中托管了页面和登录系统。 它使用了我设置的一些0Auth2内容,所以我并没有完全浪费我的努力。 谢谢大家的时间和帮助。</ p>
</ div>

this is always a connection issue from your app to Google servers. Whenever Google rejects the connection, Google will actually return an error, including a KB article. You can easily test the same connection using SMTP tester tools, or even any Outlook, phone IMAP/SMTP connection. 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).

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.

Good luck

展开翻译

译文



这始终是从您的应用到Google服务器的连接问题。 每当Google拒绝连接时,Google实际上都会返回错误,包括知识库文章。 您可以使用SMTP测试工具甚至任何Outlook,手机IMAP / SMTP连接轻松测试相同的连接。 我担心我无法帮助您处理代码,但我可以肯定地告诉您,问题不在于您的Google帐户(我已多次看到这个问题)。 </ p>

您还可以尝试使用Google的无限制服务器(ASPMX.L.GOOGLE.COM)端口25,无需验证,并且它允许在没有用户名/密码的情况下测试连接,但它只发送电子邮件 到谷歌服务器。 </ p>

祝你好运</ p>
</ div>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问