【问题标题】:phpmailer and gmail SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failedphpmailer 和 gmail SMTP 错误:无法连接到服务器:网络无法访问 (101) SMTP connect() 失败
【发布时间】:2013-12-29 16:06:01
【问题描述】:

我需要帮助 这是我的代码:

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;

$mail->SMTPSecure = "tls";
$mail->Port = 587;

$mail->Username = 'some@gmail.com';
$mail->Password = 'somepass';
$mail->addAddress('another@gmail.com', 'Josh Adams');
$mail->Subject = 'PHPMailer GMail SMTP test';
$body = 'This is the HTML message body in bold!';
$mail->MsgHTML($body);
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

我得到这个错误: 2013-12-11 15:15:02 SMTP 错误:无法连接到服务器:网络无法访问 (101) SMTP connect() 失败。邮件程序错误:SMTP 连接()失败。

有什么帮助吗?

【问题讨论】:

    标签: smtp gmail phpmailer


    【解决方案1】:

    您可能希望首先隔离此问题以确定它是否真的是网络问题;或者它是否特定于 PHP 邮件程序或您的代码。在您的服务器上,从命令提示符尝试使用 telnet 连接到端口 587 上的 smtp.gmail.com,如下所示:

    telnet smtp.gmail.com 587
    

    您应该会看到来自 smtp.gmail.com 的回复,如下所示:

    Trying 173.194.74.108...
    Connected to gmail-smtp-msa.l.google.com.
    Escape character is '^]'.
    220 mx.google.com ESMTP f19sm71757226qaq.12 - gsmtp
    

    您看到了吗,还是连接尝试挂起并最终超时?如果连接失败,这可能意味着您的托管公司阻止了端口 587 上的传出 SMTP 连接。

    【讨论】:

    • 我收到了与 220 代码相同的响应,这意味着服务器上允许使用 SMTP。那为什么我收到错误'SMTP 错误:无法连接到服务器:网络无法访问(101)SMTP 连接()失败。邮件程序错误:SMTP 连接()失败。 **我还能检查什么或此连接失败的原因是什么? **
    【解决方案2】:

    这对我有用:

    改变:

      $mail->SMTPSecure = 'ssl';
      $mail->Host = 'smtp.gmail.com';
      $mail->Port = '465';
    

      $mail->SMTPSecure = 'tls';
      $mail->Host = 'smtp.gmail.com';
      $mail->Port = '587';
    

    【讨论】:

      【解决方案3】:

      以下代码:

      $mail->SMTPSecure = 'ssl';
      $mail->Host = 'smtp.gmail.com';
      $mail->Port = 465;
      $mail->Host = 'ssl://smtp.gmail.com:465';
      

      【讨论】:

        【解决方案4】:

        我在 Godaddy 主机上遇到了同样的问题,所以我花了很多时间通过禁用服务器端的 SMTP 限制来解决它。

        SMTP 代码用于 PHPMailer

         $mail = new PHPMailer(true);
        
        try {
            //Server settings
            //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                       //Enable verbose debug output
            $mail->SMTPDebug = 2;                       //Enable verbose debug output           
            //$mail->isSMTP();                                             //Send using SMTP
        
            $mail->Host         = "tls://smtp.gmail.com";
            $mail->SMTPAuth     = true;
            $mail->Username     = contact@example.in;
            $mail->Password     = SMTP_PASSWORD;
            $mail->SMTPSecure   = "tls";
            $mail->Port         = 587;
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            //Recipients
            $mail->setFrom('contact@example.in', 'Online Order');
            $mail->addAddress('test@gmail.com');     //Add a recipient
            $mail->addReplyTo('contact@example.in', 'Jewellery');
           
        
            //Attachments
            //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
            //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name
        
            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = 'Order';
            $mail->Body    = 'This is test email content';
            $mail->AltBody = 'This is test email content';
        
            if($mail->send()){
                return '1';                 
            }else{
                return 'Order email sending failed';
            }
        

        【讨论】:

          【解决方案5】:

          改变

          $mail->SMTPSecure = "tls";

          $mail->SMTPSecure = 'ssl';

          【讨论】:

          • 这是从哪里来的?他的端口设置为 587,所以这意味着 TLS/STARTTLS。 SMTP SSL 通常具有端口 465 和 SMTP TLS/STARTTLS 587(除非您在服务器上使用一些花哨的(无法理解的)内部端口映射)。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-07-14
          • 2016-07-20
          • 1970-01-01
          • 2011-09-04
          • 2014-07-10
          • 1970-01-01
          相关资源
          最近更新 更多