douchan4674 2019-07-09 17:27
浏览 426
已采纳

如何解决:无法使用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.

  • 写回答

2条回答 默认 最新

  • douyan1903 2019-08-02 16:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器