You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SMTP连接失败求助:参照PHPMailer排障文档后问题仍未解决

无法连接Gmail SMTP服务器发送邮件问题求助

错误信息

Connection: opening to smtp.gmail.com:587, timeout=300, options=array()

Connection failed. Error #2: stream_socket_client(): unable to connect to smtp.gmail.com:587 (A connection attempt failed because the connected party did not respond properly after a period of time, or established connection failed because connected host has failed to respond.)

SMTP ERROR: Failed to connect to server: A connection attempt failed because the connected party did not respond properly after a period of time, or the established connection failed because the connected host failed to respond. (10060)

已参照PHPMailer官方排障文档调整,但仍报错,恳请协助解决。

相关代码

public function Approve1($id) 
{  
    // For database
    $data = $this->ApprovalModel->requisition_approval(array('hod'=>'submit'),$id);
    if($this->db->affected_rows() > 0)
    // PHPMailer object
    $id=$this->uri->segment(3);
    $email=$this->uri->segment(4);
    $response = false;
    $mail = new PHPMailer(); 
    // SMTP configuration
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com'; 
    $mail->SMTPDebug = 3;
    $mail->SMTPAuth = true;
    $mail->Username = 'xyz@gmail.com';   
    $mail->Password = 'xyz';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587; 
    $mail->setFrom('xyz@gmail.com', 'xxx');
    $mail->addAddress($email); 
    $mail->Subject = 'hii!'; 
    $mail->isHTML(true); 
    // Email body content
    $loginlink = site_url() . 'login' . '"';
    $mailContent = <<<HEREDOC
        Dear Sir/Madam ,
        Your staff asked for Manpower Request, please click this link for your approval $loginlink
        <p>Thank you,</p>
        <p>Cladtek HRIS</p>
        HEREDOC;
    $mail->Body = $mailContent; 
    // Send email
    if(!$mail->send()){
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
    else{ 
        $this->session->set_flashdata('flash', '<div class="alert alert-success" role="alert"> Submit Succesfully!<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
        return redirect('home/manage_data');
    }
}

排查解决建议

  • 网络连通性检查:在服务器上执行telnet smtp.gmail.com 587或curl -v smtp://smtp.gmail.com:587,确认587端口是否能正常访问。大概率是服务器防火墙/安全组封禁端口,或者运营商拦截了Gmail SMTP流量,需要对应开放端口或更换网络环境测试。
  • Gmail账号配置调整:
    1. 启用账号两步验证,生成应用专用密码替换代码中的普通密码,目前Gmail已不再支持普通密码直接登录SMTP服务。
    2. 若未开启两步验证,需开启"不太安全的应用访问"(该选项已逐步被Google淘汰,优先推荐应用专用密码方案)。
  • PHPMailer配置优化:
    1. 添加SSL临时跳过验证配置(生产环境不建议长期使用),排查证书问题:
      $mail->SMTPOptions = array(
          'ssl' => array(
              'verify_peer' => false,
              'verify_peer_name' => false,
              'allow_self_signed' => true
          )
      );
      
    2. 尝试切换到SSL端口465:将$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;和$mail->Port = 465;替换原有TLS配置。
  • 代码逻辑修复:if($this->db->affected_rows() > 0)语句缺失大括号,导致后续代码只有第一行受该条件控制,逻辑存在隐患,建议补上大括号包裹需要执行的代码块。

内容的提问来源于stack exchange,提问作者Crispy Nuts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:25:26