PHPMailer 加载一段时间,然后给出 500 - 内部服务器错误


PHPMailer loads a while, then gives 500 - Internal server error

我正在尝试设置一个php页面以自动发送验证电子邮件。 我想使用gmail smtp服务器,我看到的任何地方都建议使用PHPMailer。 我安装了它并使用了以下示例代码:

ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once ("incl'PHPMailer'PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "myemail@gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom('myemail@gmail.com','Me');
$mail->AddAddress("ToAddress@gmail.com");
$mail->Subject = "Verify your email";
$mail->Body = "Thank you for signing up.  Please verify your email using the link below:";
$mail->IsHTML (true);
if($mail->Send()){
    echo "Success";
}else{
    echo "Error";
}

尝试通过 Firefox 访问页面时,页面将加载几分钟,然后给出此错误:

500 - 内部服务器错误。
您要查找的资源有问题,无法显示。

服务器是运行IIS 7.5和PHP版本5.5.8Windows Server 2008 R2 Standard。 我可以毫无问题地访问所有其他页面,但尝试调用$mail->Send()似乎超时或其他什么。 我知道这一点,因为我评论了每一行并慢慢地重新添加片段$mail->Send()这是导致行为的行。

我的谷歌能力在这里让我失望,因为我根本无法弄清楚如何做到这一点。 关于可能出现什么问题的任何想法?

更新

我打开了服务器日志,然后尝试再次加载页面,但没有向日志添加新错误。 但是,我注意到今天在System32'LogFiles'httperr1.log中出现以下错误

2014-10-27 06:29:21 1.161.23.122 3148 212.83.145.123 80 HTTP/1.0 CONNECT mx2.mail2000.com.tw:25 400 - URL -  
2014-10-27 10:10:12 95.183.244.244 33553 212.83.145.123 80 HTTP/1.1 GET / 400 - Hostname -
2014-10-27 11:25:25 207.38.185.197 51157 212.83.145.123 80 HTTP/1.1 GET /tmUnblock.cgi 400 - Hostname -
2014-10-27 12:46:21 1.168.221.158 7952 212.83.145.123 80 - - - - - Timer_ConnectionIdle -

更新 2

我很肯定我的 gmail 帐户详细信息是正确的,并且已经测试了在服务器上使用 Thunderbird 从它发送。 尝试在没有安全方法的情况下发送时,如此评论所示,我收到此错误:

MAIL FROM 命令失败,550,5.7.3 请求的操作中止;用户未通过身份验证

我的PHP Mailer版本是5.2.9,我现在还尝试了以下方法:

  • 在文件路径中使用''而不是'
    • 无变化
  • 包括class.phpmailer.php而不是PHPMailerAutoload.php
    • Fatal error: Class 'SMTP' not found in C:'inetpub'wwwroot'incl'PHPMailer'class.phpmailer.php on line 1195
  • 通过端口 465 使用 SSL
    • SMTP connect() failed
  • 使用带有$mail->SMTPAuth = false;的端口 25 的热邮件地址发送
    • MAIL FROM command failed,550,5.7.3 Requested action aborted; user not authenticated

更新 3

重新加载问题页面后,我检查了事件查看器,并在 Windows 日志 -> 系统中看到了一个新条目:

PHP 错误:

语法错误,C:''PHP''php.ini 第 101 行的意外BOOL_TRUE

那行是:

php_flag display_errors 上

看起来您正在处理一堆问题,这里有一个清单:您在SSL,实际邮件软件和凭据方面遇到了问题。从一个主要问题变成了 3 的问题,我猜您要么没有正确输入凭据,要么没有设置您的打开 ssl,并且您在使用邮件软件时也遇到了问题。

535 535 5.7.3 身份验证不成功意味着身份验证不成功,请检查您的凭据用户名和密码。

550 错误代码,这意味着接收系统无法将您的电子邮件传递给收件人,因为邮箱不可用。

还有很多其他解决方案可以解决您的问题。你为什么不尝试更简单的东西,而不是使用自动加载PHPMailerAutoload.php。创建一个新文件并放置下面的代码,创建一个邮件正文 (test_message.html(,然后从浏览器中调用它以使用 Gmail 凭据测试 Gmail SMTP。这是PHPMailer关于如何将其与gmail smtp一起使用的片段,如果您正确填写了所有内容,则不会出错。

<?php
    require_once('../class.phpmailer.php');
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    $mail             = new PHPMailer();
    $body             = file_get_contents('test_message.html');
    $body             = eregi_replace("[']",'',$body);
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.yourdomain.com"; // SMTP server
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
    $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
    $mail->Username   = "yourusername@gmail.com";  // GMAIL username
    $mail->Password   = "yourpassword";            // GMAIL password
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    $mail->AddReplyTo("name@yourdomain.com","First Last");
    $mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }
    ?>

如果收到用户身份验证错误,则不会键入凭据正确。如果这不是问题所在,如果您未使用正确的端口,则会收到 ssl 错误。

如果包含存在软件问题,您应该尝试更好的方法。

今天早上我正在辩论我是否应该PHPMailer,最终使用了Swiftmailer,它在我用PHPComposer安装它后立即工作。

我的内部邮件服务器非常挑剔,在硬件和软件级别上有大量的防火墙设置和规则,我很惊讶它没有给我带来任何问题; 电子邮件立即发送,端口 587 没有任何问题。

require_once 'vendor/swiftmailer/swiftmailer/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('mail.hellog***.com', 587)
  ->setUsername('apache@hellog***.com')
  ->setPassword('apa******')
  ;

这是您对 gmail 的需求:

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername($this->username)
  ->setPassword($this->password);
$this->mailer = Swift_Mailer::newInstance($transporter);

如果你不喜欢什么,你可以试试phpgmailer:https://github.com/GregDracoulis/LectureBank/tree/master/phpgmailer

下面是如何使用它的示例:http://www.vulgarisoverip.com/2006/10/13/update-send-email-with-php-and-gmail-hosted-for-your-domain/

本课程专用于 Gmail 使用。

如果您的所有问题都与 openSSL 有关,则应按照以下步骤操作:

1. 确保 OpenSSL 模块 DLL 文件包含在 PHP 安装中:

C:''user>dir ''local''php''ext''php_openssl.dll

C:''local''php''ext 目录

php_openssl.dll

2. 创建 PHP 配置文件,如果不存在,'local'php'php.ini

C:'user>copy 'local'php'php.ini-production 'local'php'php.ini
    1 file(s) copied.

3. 通过使用文本编辑器编辑配置文件来启用 OpenSSL 模块。您需要删除评论制作者(;)在两条配置线上:

...
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "ext"
...
extension=php_openssl.dll
...

就是这样。您应该在Windows IIS WebServer上设置openSSL的所有设置。按照这些选项和步骤操作,这可能是您的问题。

这个问题是斜杠 ( '' (

编辑

require_once ("incl''PHPMailer''PHPMailerAutoload.php");

我已经尝试过你的代码,我只更改了这一行:

require_once ("incl'PHPMailer'PHPMailerAutoload.php");

通过这个:

require_once ("incl'PHPMailer'class.phpmailer.php");

它已成功工作.
此外,您还应该注意您的 PHP 发布和 PHP 版本.
您是否尝试使用不安全的方法发送电子邮件?
您确定您的谷歌凭据有效吗?

已更新
从服务器的命令行中,此命令的输出是什么以及响应需要多少时间

telnet smtp.gmail.com 587

想知道您是否遇到了这个问题遇到的相同问题。谷歌积极反对人们使用Gmail作为自己的个人SMTP服务器。

http://www.labnol.org/internet/email/gmail-daily-limit-sending-bulk-email/2191/

Google 还限制了您一天内可以通过 Gmail 帐户发送的电子邮件数量。如果您超出每日配额,Google 可能会暂时停用您的 Gmail 帐户,而不会发出任何警告,您可能需要等待最多 24 小时才能重新获得对 Gmail 邮箱的访问权限。

我可以看到这样的错误可能会产生 500 错误。

从评论到问题,似乎很明显这个问题与OpenSSL有关。

提到您已经取消了extension=php_openssl.dll注释,但phpinfo()确实表明它没有加载。

一些想法:

  • 取消注释 openssl php 模块后,您是否重新启动了网络服务器php.ini

    这是非常明显的一个,但仍然值得一提。php.ini在启动时加载,不执行服务重新启动只是一个常见的错误。

  • 检查Loaded Configuration File的值phpinfo()

    如果此位置与您编辑的php.ini位置不同,则不清楚为什么没有加载它。此外,以下几行phpinfo()可能值得检查。

    • 配置文件 (php.ini( 路径
    • 加载的配置文件
    • 扫描此目录以查找其他.ini文件

还有其他事情可以做,比如卸载和重新安装OpenSSL,但最好先检查这些更明显的事情。

进一步的想法:

  • 检查错误日志

    对于 php,路径可以(再次(在您的phpinfo()中找到。查找error_log的值并在您尝试重新加载页面后立即检查最新条目。

    此外,您可能还需要检查 IIS 的日志文件。如果您不知道 IIS 管理器中的路径查找,请在左窗格中选择计算机,然后在中间窗格中,转到 IIS 区域中的"日志记录"下。通常,这些会指向类似C:'inetpub'logs'LogFilesC:'Windows'System32'LogFiles的内容,其中包含一个子文件夹,例如W3SVC[x] [x]是网站ID。 (如果您只有一个页面,则只是1,否则请检查IIS管理器中的顶级网站文件夹,在右侧窗格中查看站点列表, 它是应用 ID(

设置和配置 IIS

首先转到服务器上的服务器管理器。大多数情况下,这是您将在其上运行php网站的服务器。转到功能摘要部分中的添加功能。 在添加功能向导中检查SMTP服务,然后点击安装。现在等待安装完成。在"开始"菜单中的"管理工具"-> Internet 信息服务 6.0 下打开 IIS6(或 IIS7(管理器,SMTP 的配置将使用 IIS6 中的管理控制台。 在 [SMTP 虚拟服务器] 下,单击鼠标右键,然后从上下文菜单中选择属性。转到访问选项卡并点击中继按钮。通过单击"添加"按钮将新条目添加到列表中,然后在单台计算机输入字段中输入 127.0.0.1(或 Web 服务器的 IP(。点击确定两次以应用新设置。

配置 php

要使 Php 发送电子邮件,您需要进行一些配置。基本上有两种选择。如果您只打算在单个项目中使用 SMTP 服务器并且不希望它干扰其他项目,则可以通过在发送电子邮件之前在任何地方添加以下行来从 php 代码中设置配置:

ini_set('SMTP','localhost' ); 
ini_set('sendmail_from', 'administrator@YourWebsite.com');

另一种方法,要全局使用这些设置,请将下一行添加到 php.ini 文件中。

[mail function]
SMTP = localhost
sendmail_from = me@example.com

请确保在进行这些更改后重新启动服务器,以确保它们已加载。

此处提供了此解决方案 http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx 有关如何设置 IIS 和 php 的解答,并作为此问题的答案提供 尝试使用 MAIL 功能时出现 500 错误

尝试对端口 465 使用 SSL 加密:

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

警告:require_once(邮件/PHPMailer自动加载.php(:无法打开 流:中没有这样的文件或目录。

这是我路由文件的方式

以前: require_once "mailer/PHPMailerAutoload.php";

后: require_once "../mailer/PHPMailerAutoload.php";

。与通常使用 css 将 img 链接为后台的过程相同。