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

时间:2009-12-22 07:06:09

标签: php html email phpmailer

当我尝试通过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.';
}
?>

17 个答案:

答案 0 :(得分:41)

在Ubuntu(至少12.04)中,默认情况下似乎没有安装sendmail。您必须使用该命令安装它 sudo apt-get install sendmail-bin

如上所述,您可能还需要为其配置适当的权限。

答案 1 :(得分:12)

我使用了这行代码

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 - 函数内。

答案 2 :(得分:6)

我刚刚遇到这个问题,并在我的apache错误日志中发现sendmail没有安装,安装后它全部正常工作!

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

答案 3 :(得分:3)

有同样的问题。刚刚快速查找了apache2 error.log文件,它确切地说出了问题所在:

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

因此,解决方案是为/usr/sbin/sendmail文件提供适当的权限(无法从php访问)。

执行此操作的命令是:

> chmod 777 /usr/sbin/sendmail

确保它甚至存在!

答案 4 :(得分:2)

确保您还包含phpmailer附带的smtp类:

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

答案 5 :(得分:2)

要重新访问一个旧线程,我的问题是其中一个“AddressTo”电子邮件地址无效。删除该电子邮件地址会删除错误。

答案 6 :(得分:2)

尝试使用 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';

答案 7 :(得分:1)

尝试使用非gmail的地址。他们不允许(据我所知)轻松访问发送邮件。上周我正在做一个简单的邮件程序,他们也不使用默认端口发送,并要求你通过https传输

答案 8 :(得分:1)

如上所述here,“这意味着您的PHP安装未配置为正确调用mail()函数(例如,您的php.ini中未正确设置sendmail_path),或者您没有本地邮件服务器安装和配置。“

在我的情况下,我必须在我的webhost设置中允许mail()函数(“激活mail()队列”)。

答案 9 :(得分:1)

与您的主机核实,看看他们是否对发送的电子邮件有任何每小时限制。

答案 10 :(得分:1)

这是一个系统错误。

使用以下方法检查系统错误:

tail /var/log/httpd/error_log

可能是任何原因。

答案 11 :(得分:0)

我已经解决了我的问题(对于wamp)

    $mail->IsSMTP(); 

    $mail->Host='hote_smtp'; 

of corse用正确的值改变hote_smtp

答案 12 :(得分:0)

一个老线程,但它可能会帮助像我这样的人。 我通过在PHP.ini

中将SMTP服务器值设置为合法值来解决了该问题

答案 13 :(得分:0)

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

print_r(error_get_last());

这将打印出导致默认php mail()中断的确切错误。

希望它对某人有帮助。

答案 14 :(得分:0)

我有同样的错误。回复引起了这个问题。我删除了它。

$email->AddReplyTo( $admin_email, $admin_name ); 

答案 15 :(得分:0)

“无法实例化邮件功能”是PHPMailer报告对mail()(在Mail扩展中)的调用失败的方式。 (所以你正在使用'邮件'邮件。)

您可以尝试在PHPMailer :: MailSend中调用mail()之前删除@s,并查看哪些错误(如果有)被静默丢弃。

答案 16 :(得分:0)

在CentOS中,这可能是由SELinux政策引起的。 执行以下代码以查看是否已启用。

getsebool httpd_can_sendmail

您可以通过调用以下命令启用它。 -P参数使其成为永久性的。

setsebool -P httpd_can_sendmail 1