无法实例化邮件功能。为什么会出现这个错误?

21

当我尝试通过PHPMailer发送邮件时,出现以下错误信息。我的代码如下:

<?
require("phpmailer/class.phpmailer.php"); // First we require the PHPMailer libary in our script
$mail = new PHPMailer(); // Next we create a new object of the PHPMailer called $mail
$mail->From = "rajasekar.kcet@gmail.com";
$mail->FromName = "Rajasekar";
$mail->AddAddress("rajasekar.kcet@gmail.com"); // This is the adress to witch the email has to be send.
$mail->Subject = "First PHP Email message"; // This is the subject  of the email message.
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHP."; // This is the actual email message
if(!$mail->Send()) // Now we send the email and check if it was send or not.
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Message has been sent.';
}
?>

2
你收到了什么错误信息? - Pekka
1
无法实例化邮件功能 @Pekka - Rajasekar
@Rajasekar - 这听起来不像是一个真正的错误信息,因为函数无法被实例化。你能复制粘贴出确切的错误输出吗? - zombat
1
邮件发送错误 - 无法实例化邮件功能。 - Rajasekar
在IIS中,php不使用二进制邮件程序,而是依赖于已安装的MTA(即您需要针对SMTP服务器)。 - Triynko
对我来说,这是由服务器问题引起的。当我尝试更新系统(apt-get update)时,通过SSH收到了分段错误消息,因此我重新启动了VPS,PHPMailer正常工作。 - Harkály Gergő
17个回答

43
在Ubuntu(至少在12.04版本中),似乎默认没有安装sendmail。您需要使用以下命令来安装它:sudo apt-get install sendmail-bin 您可能还需要按照上面提到的方法配置它的适当权限。

2
在14.04.4中,sudo apt-get install sendmail似乎可以解决问题。 - Jeff Puckett

13

我使用了这一行代码

if($phpMailer->Send()){

    echo 'Sent.<br/>';

}else{

    echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';

}

为了找出问题所在。结果发现,我是在安全模式下运行的,在第770行或者那里,有一个第五个参数$params被传递给mail(),但是在安全模式下不支持这样做。我只需要将其注释掉,然后就可以正常工作了:

$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header/*, $params*/);

这是在 PHPMailer 的 MailSend 函数中。


7

我刚刚遇到了这个问题,在我的Apache错误日志中发现sendmail未安装,安装后一切正常工作!

root@web1:~$ tail /var/log/apache2/error.log
sh: 1: /usr/sbin/sendmail: not found

3

我遇到了同样的问题。只需快速查看apache2 error.log文件,它就能准确地指出问题所在:

> sh: /usr/sbin/sendmail: Permission denied

因此,解决方案是为/usr/sbin/sendmail文件赋予适当的权限(php无法访问该文件)。
执行此操作的命令如下:
> chmod 777 /usr/sbin/sendmail

一定要确定它是否存在!


3
你确定要给整个世界可写的sendmail访问权限吗?我认为这可能会成为一个漏洞。也许可以尝试仅授予可执行访问权限:chmod +x /usr/sbin/sendmail - Jeff Puckett

3

尝试使用SMTP发送电子邮件:

$mail->IsSMTP();
$mail->Host = "smtp.example.com";

// optional
// used only when SMTP requires authentication  
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';

2

请确保您还包含了与phpmailer一起提供的smtp类:

// for mailing
require("phpmailer/class.phpmailer.php");
require("phpmailer/class.smtp.php");

2

重新查看旧线程,我的问题是“AddressTo”电子邮件地址中有一个无效的地址。删除该电子邮件地址即可删除该错误。


2

重新访问旧线程,您可以通过添加以下内容深入调试PHPMailer:

print_r(error_get_last());

这将为您打印出导致默认的php mail()出现故障的确切错误。希望能对某些人有所帮助。

这帮了我!以下是从 print_r 输出的完整错误信息:Array ( [type] => 2 [message] => mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() [file] => ...\vendor\phpmailer\phpmailer\src\PHPMailer.php [line] => 852 ) - dmikester1
所以你的SMTP配置不正确。由于错误显示,您可以通过在php.ini中提及端口、主机、用户名和其他凭据或通过ini_set()内联指定它们来配置SMTP服务器设置。我建议阅读SMTP文档以正确设置它。一点点谷歌搜索肯定会有帮助! - Shuja Ahmed
1
谢谢,我原本以为PHPMailer可以自己处理SMTP。现在我正在使用我的Gmail SMTP凭据,它已经可以工作了。 - dmikester1

1

尝试使用非 Gmail 地址。据我所知,他们不允许使用 SMTP 访问发送邮件。上周我正在编写一个简单的邮件程序,他们也不使用默认端口进行发送,并要求您通过 HTTPS 进行传输。


1
正如此处所述,“这意味着您的PHP安装未正确配置调用mail()函数(例如,在php.ini中sendmail_path未正确设置),或者您没有安装和配置本地邮件服务器。”在我的情况下,我需要在我的Web主机设置中允许mail()函数(“激活mail()队列”)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接