【问题标题】:Phpmailer using smtp with Gmail not working - connection timing out使用 smtp 和 Gmail 的 Phpmailer 无法正常工作 - 连接超时
【发布时间】:2013-10-13 12:35:16
【问题描述】:

我查看了以下链接:

phpmailer send gmail smtp timeout

send email using Gmail SMTP server through PHP Mailer

http://uly.me/phpmailer-and-gmail-smtp/

...并尝试为自己实现这些组合但是...大多数时候它会发送此消息...

消息无法发送。

邮件程序错误:SMTP 连接()失败。

但是有一次当我在“tls”和“ssl”之间进行实验时它发送了这个......

SMTP 错误:无法连接到服务器:连接超时 (110) SMTP connect() 失败。 无法发送消息。

邮件程序错误:SMTP 连接()失败。

我的代码已附上...我是否遗漏了什么?我询问了网络托管服务他们是否阻止并给了他们我的代码模板 - 他们说服务器允许连接到 Gmail 的 SMTP。

    require_once("class.phpmailer.php");
    $mail = new PHPMailer();
    $mail -> IsSMTP();
    $mail -> SMTPDebug = 2;
    $mail -> SMTPAuth = 'true';
    $mail -> SMTPSecure = 'tls';
    $mail -> SMTPKeepAlive = true;
    $mail -> Host = 'smtp.gmail.com';
    $mail -> Port = 587;
    $mail -> IsHTML(true); 

    $mail -> Username = "myemail@gmail.com";
    $mail -> Password = "mypassword";
    $mail -> SingleTo = true; 

    $to = xxx;                           
    $from = xxx;
    $fromname = xxx;
    $subject = xxx;
    $message = xxx
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";

    $mail -> From = $from;
    $mail -> FromName = $fromname;
    $mail -> AddAddress($to);

    $mail -> Subject = $subject;
    $mail -> Body    = $message;

    if(!$mail -> Send()){
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail-> ErrorInfo;
        exit;
    }

【问题讨论】:

  • 我还研究了link,它没有帮助,因为我尝试了 ssl 和 tls,它给出了相同的输出
  • 我编辑了我的答案,看看你是否错过了它

标签: php email ssl gmail phpmailer


【解决方案1】:

从以下位置下载适用于 Windows 的 sendmail http://www.glob.com.au/sendmail/sendmail.zip 将sendmail.exe和sendmail.ini复制到C:/usr/lib/

编辑 sendmail.ini 并输入您的邮件帐户凭据。

您可能还想配置这 2 个字段(或者发送可能不起作用) force_sender=you-sender@yourdomain.com force_recipient=you@yourdomain.com 顺便说一句,我取消了 debug_logfile 的注释,这样我就可以看到哪些数据正在发送到我的 SMTP 服务器。

编辑 c:\php\php.ini

sendmail_from = you@yourdomain.com

;仅适用于 Unix。您也可以提供参数(默认值:“sendmail -t -i”)。 sendmail_path = C:/usr/lib/sendmail.exe -t -i

重启apache 从 [开始] > 运行 > C:/usr/lib/sendmail.exe 启动 sendmail.exe 或在 Windows 资源管理器中转到 C:/usr/lib,然后双击 exe 文件。

而这个解决方案出现在Sendmail Wamp Php

我现在在 Windows 10 上尝试过,它使用 gmail 帐户运行

添加:

打开 CMD 并使用发送邮件作为守护程序/服务

sc create SendMail binpath= "C:\usr\lib\sendmail.exe"

现在,请确保您的系统上安装了 OpenSSL,您可以从这里下载它:https://slproweb.com/products/Win32OpenSSL.html

安装您需要的版本,不要记得安装“文件到 Windows 文件夹”。重新启动并重试,您将解决它。

【讨论】:

    【解决方案2】:

    添加

    date_default_timezone_set('Etc/UTC');
    

    在包括自动加载器之前。 SMTP 需要设置时区,这是我的问题。

    【讨论】:

      【解决方案3】:

      这个问题有很多重复,所以这里是一个固定的答案:

      1. 您的代码基于the gmail example provided with PHPMailer
      2. 查看the troubleshooting docs
      3. 注意this issue,与 Larry K 所说的有关。

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题并解决了:

        首先,在phpmailer中开启smtp错误日志:

            $mail->SMTPDebug  = 2;       // enables SMTP debug information (for testing)
        

        然后重试您的 phpmailer 电子邮件发送。您将在标准错误输出中看到整个 SMTP 对话。如果您使用的是网络服务器,请查看网络服务器日志文件。

        然后我可以看到来自 gmail 的错误响应。 Gmail 不接受登录。

        smtp 对话中的错误是指article。它提供了一些提示:

        1. Allow less secure apps to use your account
        2. 从网络浏览器登录到 gmail。
        3. 确认gmail login captcha。 (它实际上可能不会向您显示验证码,但这是突然允许我的电子邮件通过的附加步骤。)

        【讨论】:

          【解决方案5】:

          这可能是 Gmail 阻止了您的访问。

          转到您的安全配置,看看它是否阻止任何访问..

          或者尝试更改您的密码,然后重试。

          【讨论】:

            【解决方案6】:

            我深入研究了它。使用 php 原生的 fsocketopen 来测试连接并消除大部分潜在问题。用这个写一个简单的php页面:

                $host = "smtp.gmail.com";
                $port = "587";
                $checkconn = fsockopen($host, $port, $errno, $errstr, 5);
                if(!$checkconn){
                    echo "($errno) $errstr";
                } else {
                    echo 'ok';
                }
            

            你应该返回“ok”。如果不是,你知道你有一个与 Phpmailer 无关的连接问题。如果是这种情况,它可能是托管公司。如果不是,那可能是本地主机和托管公司之间的区别很简单,比如不同版本的 php。

            我怀疑这个脚本不会建立连接

            【讨论】:

            • 很酷的凯西!!!它说“好的”......这意味着它是托管公司的问题。无论如何,我现在正在尝试使用 ssl... 错误现在是 SMTP -> ERROR: Failed to connect to server: Connection timed out (110)SMTP Connect() failed
            • 这意味着托管公司没有阻止端口。一定是phpMailer的一些配置问题
            • 我遇到了类似的阻塞/未阻塞问题。在此处链接以获取可见性stackoverflow.com/questions/32827193/…
            【解决方案7】:

            检查以确保您可以从您的虚拟主机访问 gmail。我假设它是Linux。 SSH 输入和命令行输入

            telnet smtp.gmail.com 587
            

            你应该回来

            Connected to smtp.something 
            

            它必须是 localhost 和您的提供者之间的配置差异

            【讨论】:

            • 这很有趣,Casey - 虽然不知道如何做到这一点......对于 gmail 的 localhost 连接,代码有效......至于来自我的远程 Web 主机的连接 - 他们告诉我他们的服务器允许与 gmail 的连接。我要澄清配置吗?
            • 你能通过 ssh 进入你的网络服务器吗?您唯一需要从他们那里知道的是端口 587 是否被阻塞。如果他们告诉你它不是那并不一定意味着它不是。您使用的是什么虚拟主机,它是共享主机吗?
            • 它是共享主机——您每月只需不到 5 美元即可获得。 Webhostingpad
            • 我研究了它here,所以你不付费就无法获得 SSH。
            【解决方案8】:

            使用 ssl

            $mail -> SMTPSecure = 'ssl';
            

            端口应为 465

            $mail -> Port = 465;
            

            将您的主机更改为:

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

            希望它有效

            【讨论】:

            • 我已经尝试过这个@vidhu - 似乎它并没有解决问题。我认为这不是ssl和tls之间的问题......
            • @redber2009 好吧,那么我恐怕要说它是你的主机正在运行。 :S
            • 这解决了我的问题! (smtp错误)谢谢!我刚刚将我的端口从 587 更改为 465 :)
            • 这也解决了我的问题。但是为什么需要“ssl://”呢?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-10
            • 2017-10-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多