【问题标题】:PHPMailer loads a while, then gives 500 - Internal server errorPHPMailer 加载一段时间,然后给出 500 - 内部服务器错误
【发布时间】:2014-10-27 00:22:31
【问题描述】:

我正在尝试设置一个 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 - 内部服务器错误。
您要查找的资源有问题,无法显示。

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

我的 Google 能力在这里让我失望,因为我根本不知道如何完成这项工作。关于可能出错的任何想法?

更新

我打开了服务器日志,然后尝试再次加载页面,但日志中没有添加新的错误。但是,我从今天开始在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 测试了从它发送的信息。当尝试在没有安全方法的情况下发送时,如 this comment 中所建议的,我收到此错误:

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
  • 通过端口 25 使用 hotmail 地址发送 $mail->SMTPAuth = false;
    • MAIL FROM command failed,550,5.7.3 Requested action aborted; user not authenticated

更新 3

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

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

那一行是:

php_flag display_errors on

【问题讨论】:

  • 错误 500 本身没有意义。您应该在 Web 服务器日志中找到有意义的错误消息。要么,要么在您的脚本或 php.ini 中将 display_errors 设置为 true 以使其可见。这不是 PHPMailer 特有的——对于任何 PHP 致命错误都是如此。
  • 我尝试将ini_set('display_errors', 1); 添加到脚本的顶部,但它并没有改变输出。使用日志文件中的一些详细信息更新了问题。
  • 如果您在浏览器中看到 500 个错误,但没有记录任何内容,则说明您的 Web 服务器配置有问题。您显示的错误是 400(错误请求)错误。您还应该尝试打开错误报告:error_reporting(E_ALL);
  • @Synchro 我尝试在页面上添加ini_set('error_reporting', 'E_ALL'); 以及在php.ini 中添加error_reporting = E_ALL,但都没有更改错误消息。你能指出我如何验证服务器配置的正确方向吗?
  • error_reporting 是一个函数 - 完全使用我发布的内容 - 你输入的内容本身会导致 500 错误!

标签: php iis-7.5 phpmailer windows-server-2008-r2


【解决方案1】:

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

535 535 5.7.3 Authentication Unsuccessful表示身份验证不成功,请检查您的凭据用户名和密码。

550错误码,表示由于邮箱不可用,接收系统无法将您的邮件投递给收件人。

还有很多其他解决方案可以解决您的问题。为什么不尝试更简单的方法而不是使用自动加载 PHPMailerAutoload.php。创建一个新文件并将代码放在下面,创建一个消息正文 (test_message.html) 并从浏览器调用它以使用 gmail 凭据测试 gmail smtp。这是来自 PHPMailer 的关于如何将其与 gmail smtp 一起使用的 sn-p,如果您正确填写了所有内容,它应该不会出错。

<?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 错误。

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

今天早上我在争论是否应该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>目录\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。请遵循这些选项和步骤,这可能是您的问题。

【讨论】:

  • 我知道我的凭据是正确的,因为我可以从其他任何地方登录该帐户并发送和接收电子邮件,这包括服务器上存在问题的 Mozilla Thunderbird 客户端。我没有 extension_dir = "ext" 未注释,但在启用它并运行 iisreset 后,输出是相同的。尝试 install Swiftmailer 会出现 UPDATE 3 中提到的错误。 PHPGMailer 给Fatal error: Call to undefined method PHPGMailer::From().
  • 你的 php 版本是什么,你是如何包含这些文件的?您是否在 php 脚本可访问的目录中正确设置了 lib?
  • PHP 版本 5.5.8 并包括使用 require_once('incl\phpGMailer\class.phpgmailer.php'); 这是我放置这 3 个文件的位置。 PHPMailer 与我认为可以正常工作的问题相同,因为页面在$mail-&gt;Send(); 之前都很好
  • 所以你检查了你的服务器上是否安装了openssl?
  • C:\php\ext\ 包含 php_openssl.dll 和文件夹 opensslphpinfo() 声明:OpenSSL support | disabled (install ext/openssl)
【解决方案2】:

这个问题是斜线(\)

编辑

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

【讨论】:

  • 更改代码使用 \\ 而不是 \ 并得到同样的问题。
【解决方案3】:

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

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

这个:

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

它成功了。
此外,您应该注意您的 PHPMailer 版本和您的 PHP 版本。
您是否尝试过使用不安全的方法发送电子邮件?
您确定您的 Google 凭据有效吗?

更新
从你的服务器的命令行,这个命令的输出是什么以及需要多少时间来响应

telnet smtp.gmail.com 587

【讨论】:

  • 进行建议更改会导致此错误:Fatal error: Class 'SMTP' not found in C:\inetpub\wwwroot\incl\PHPMailer\class.phpmailer.php on line 1195 至于您的其他问题,我将使用这些详细信息更新我的问题。
  • 来自 telnet 命令的响应:200 mx.google.com ESMTP i15sm10941378wjq.22 - gsmtp
【解决方案4】:

我想知道您是否遇到了 this question 遇到的相同问题。 Google 非常反对将 Gmail 用作自己的个人 SMTP 服务器的人。

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

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

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

【讨论】:

  • 我怀疑这是问题所在,因为我什至无法发送一封电子邮件,但很高兴知道。
【解决方案5】:

从 cmets 到问题,很明显这个问题与 OpenSSL 相关。

您提到您已经取消了 extension=php_openssl.dll 的注释,但 phpinfo() 确实显示它未加载。

一些想法:

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

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

  • 检查phpinfo()Loaded Configuration File的值

    如果这与您编辑的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管理器中的顶级网站文件夹,在右侧窗格中查看网站列表,它是App ID)

【讨论】:

  • 我记得多次运行iisreset,但只是为了确保我确保该行未注释并再次运行。不用找了。至于你的第二点:Loaded Configuration File: C:\PHP\php.ini这和我一直在编辑的一样。 Configuration File (php.ini) Path: C:\Windows(这可能是问题所在?)Scan this dir for additional .ini files: (none)
  • @DavidStarkey:由于您的Loaded Configuration File 是您正在编辑的那个,这应该没问题(尽管您可以尝试将其复制到 Windows 路径等,但它确实应该(!)没有轴承)。 IISReset 也可以重新启动您的 Web 服务。我添加了关于错误日志的第三个要点……您可能需要检查这一点。
  • phpinfo() 的日志记录是error_log | no value | no value。至于 IIS 错误日志,我似乎没有选项...添加了有关事件查看器输出的一些详细信息。
  • 另外,尝试使用error_log = "C:\Windows\temp\php_errors.log" 设置位置不会改变输出。
  • 哇,这真的是一个艰难的oO。好吧,错误日志会非常有用,所以让我们先解决这个问题。您是否也检查过上面提到的这两个位置(C:\inetpub\logs\...C:\Windows\System32\LogFiles\W3SVC1)?至于 php 错误日志,您是否还设置了log_errors = On 和可能的error_reporting = E_ALL(但这并不重要,因为您一开始也已在 php 代码中设置了它)。
【解决方案6】:

设置和配置 IIS

首先转到您服务器上的 ServerManager。大多数情况下,这是您将在其上运行 php 网站的服务器。转到功能摘要部分中的添加功能。在添加功能向导中检查 SMTP 服务并点击安装。现在等待安装完成。在开始菜单的管理工具 -> Internet Information Services 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,并作为这个问题的答案提供500 error when trying to use the MAIL function

【讨论】:

  • 解决方案是这个答案stackoverflow.com/questions/15231214/…的一部分,所以如果这是正确的,也许它应该被标记为重复问题。
  • 试试这个,我不断收到mail(): SMTP server response: 550 5.7.3 Requested action aborted; user not authenticated
【解决方案7】:

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

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

【讨论】:

  • if( $mail-&gt;Send() ) 检查失败。
  • 这是从 Gmail 文档中提取的如何为 SMTP 配置外部电子邮件客户端:外发邮件 (SMTP) 服务器 - 需要 TLS 或 SSL:smtp.gmail.com;使用身份验证:是; TLS/STARTTLS 端口:587; SSL 端口:465
  • 这很好,但它仍然不适合我。是否有一个链接可以指向这个可能会更详细?
  • 我建议暂时在 Outlook 或 Thunderbird 中设置一个测试 gmail 帐户来测试 SMTP 设置。至少它在我的 MS Outlook 2007 中使用 SSL 和 465 端口工作。如果同样的错误,也许你必须检查防火墙或本地 ISP。
  • 请不要使用旧版本。根据 PHPMailer 文档和示例,Gmail 应该在 587 上使用 tls。应避免使用 465 上的 ssl。
【解决方案8】:

警告:require_once(mailer/PHPMailerAutoload.php):打开失败 流:在....中没有这样的文件或目录

这就是我路由文件的方式

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

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

...与通常使用 css 将 img 链接为背景的过程相同。

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多