4

我查看了以下链接:

phpmailer 发送 gmail smtp 超时

通过 PHP Mailer 使用 Gmail SMTP 服务器发送电子邮件

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;
    }
4

8 回答 8

8

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

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

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

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

于 2013-10-08T23:19:42.667 回答
3

我有同样的问题并解决了它:

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

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

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

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

smtp 对话中的错误与文章有关。它提供了一些提示:

  1. 允许不太安全的应用程序使用您的帐户
  2. 从网络浏览器登录 gmail。
  3. 确认gmail 登录验证码。(它实际上可能不会向您显示验证码,但这是突然允许我的电子邮件通过的附加步骤。)
于 2014-10-20T07:27:50.747 回答
2

Use ssl

$mail -> SMTPSecure = 'ssl';

Port should be 465

$mail -> Port = 465;

Change your host to:

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

Hopefully it works

于 2013-10-07T03:11:28.120 回答
1

Check to make sure you can reach gmail from your webhost. I'm assuming it's linux. SSH in and on the command line type

telnet smtp.gmail.com 587

You should get back

Connected to smtp.something 

It has to be a configuration difference between localhost and your provider

于 2013-10-07T03:13:49.797 回答
1

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

  1. 将您的代码基于PHPMailer 提供的 gmail 示例
  2. 查看故障排除文档
  3. 请注意这个问题,与拉里 K 所说的有关。
于 2015-02-24T15:20:46.900 回答
0

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

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

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

于 2014-07-11T20:10:33.807 回答
0

添加

date_default_timezone_set('Etc/UTC');

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

于 2015-11-15T19:21:30.123 回答
-2

从http://www.glob.com.au/sendmail/sendmail.zip下载适用于 Windows 的 sendmail 将 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 或转到 C:/usr/lib 在 Windows 资源管理器中启动 sendmail.exe,然后双击 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 文件夹”。重新启动并重试,您将解决它。

于 2016-03-28T15:30:05.237 回答