调试 PHP Mail() 和/或 PHPMailer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2896280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging PHP Mail() and/or PHPMailer
提问by Agos
I'm quite stuck with a problem sending mail from a PHP script. Some data:
我在从 PHP 脚本发送邮件时遇到了问题。一些数据:
- Shared hosting, no SSH access, only hosting provider panel
- PHP version 5.2.5
- Last year I built a site which had no problems sending mail with the same hosting
- Let's say the domain is “domain.com” and my private address is “[email protected]” for anonimity's sake in the following code.
- 共享主机,无 SSH 访问,只有主机提供商面板
- PHP 版本 5.2.5
- 去年我建立了一个网站,使用相同的主机发送邮件没有问题
- 在下面的代码中,为了匿名,假设域是“domain.com”,我的私人地址是“[email protected]”。
Here's the code:
这是代码:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$to = "[email protected]";
$subject = "Hi";
$body = "Test 1\nTest 2\nTest 3";
$headers = 'From: [email protected]' . "\r\n" .
'errors-to: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $body, $headers)) {
echo("Message successfully sent");
} else {
echo("Message sending failed");
}
require('class.phpmailer.php');
$message = "Hello world";
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->AddAddress("[email protected]", "Agos");
$mail->SetFrom("[email protected]","My Site");
$mail->Subject = "Test Message";
$mail->Body = $message;
$mail->Send();
?>
And here is what I get:
这是我得到的:
Message sending failed Could not instantiate mail function.
消息发送失败 无法实例化邮件功能。
Which is baffling to say the least. Is there anything I can do to get at least some more meaningful errors? Why is code from the class showing up in my file?
至少可以说这是令人费解的。我能做些什么来至少获得一些更有意义的错误吗?为什么类中的代码会显示在我的文件中?
回答by Stacey Richards
It looks like the class.phpmailer.php file is corrupt. I would download the latest versionand try again.
看起来 class.phpmailer.php 文件已损坏。我会下载最新版本,然后再试一次。
I've always used phpMailer's SMTP feature:
我一直在使用 phpMailer 的 SMTP 功能:
$mail->IsSMTP();
$mail->Host = "localhost";
And if you need debug info:
如果您需要调试信息:
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only

