使用smtp的Phpmailer与Gmail不起作用 - 连接超时


Phpmailer using smtp with Gmail not working - connection timing out

我查看了以下链接:

phpmailer send gmail smtp timeout

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

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

。并试图为自己实现这些的组合,然而......大多数情况下,它会发送此消息...

无法发送消息。

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

但是,当我在">

tls"和"ssl"之间进行实验时,有一次它发送了这个......

SMTP

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

邮件程序错误: 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;
    }

我深入研究了它。使用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。

我怀疑这个脚本不会建立联系

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

首先,在 phpmailer 中打开 smtp 错误日志记录:

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

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

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

SMTP 对话中的错误是指一篇文章。它提供了一些提示:

  1. 允许安全性较低的应用使用您的帐户。
  2. 从网络浏览器登录 Gmail。
  3. 确认 gmail 登录验证码。(它实际上可能不会向您显示验证码,但这是突然允许我的电子邮件通过的额外步骤。

使用 ssl

$mail -> SMTPSecure = 'ssl';

端口应为 465

$mail -> Port = 465;

将您的主机更改为:

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

希望它有效

检查以确保您可以从虚拟主机访问 Gmail。我假设它是 linux。在命令行类型中和在命令行类型上使用 SSH

telnet smtp.gmail.com 587

你应该回来

Connected to smtp.something 

它必须是本地主机和您的提供商之间的配置差异

这个问题有很多重复项,所以这里有一个罐头答案:

  1. 基于PHPMailer提供的gmail示例编写代码
  2. 查看故障排除文档
  3. 注意这个问题,与拉里K所说的话有关。

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

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

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

添加

date_default_timezone_set('Etc/UTC');

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

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

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

您可能还想配置这两个字段(否则发送可能不起作用(force_sender=you-sender@yourdomain.comforce_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

重启阿帕奇启动 sendmail.exe从 [开始]>运行> C:/usr/lib/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文件夹"。重新启动并重试,您将解决它。