php 无法实例化邮件功能。为什么会出现这个错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1944631/
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
Could not instantiate mail function. Why this error occurring
提问by Rajasekar
When I'm trying to send mail through PHPMailer, i'm getting this error message. My code is below:
当我尝试通过 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 = "[email protected]";
$mail->FromName = "Rajasekar";
$mail->AddAddress("[email protected]"); // 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.';
}
?>
回答by Michael Yagudaev
In Ubuntu (at least 12.04) it seems sendmail is not installed by default. You will have to install it using the command
sudo apt-get install sendmail-bin
在 Ubuntu(至少 12.04)中,默认情况下似乎没有安装 sendmail。您必须使用以下命令安装它
sudo apt-get install sendmail-bin
You may also need to configure the proper permissions for it as mentioned above.
您可能还需要如上所述为其配置适当的权限。
回答by arik
I used this line of code
我用了这行代码
if($phpMailer->Send()){
echo 'Sent.<br/>';
}else{
echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';
}
to find out what the problem was. Turns out, I was running in safe-mode, and in line 770 or something, a fifth argument, $params, was given to mail(), which is not supported when running in safe-mode. I simply commented it out, and voilá, it worked:
找出问题所在。原来,我是在安全模式下运行的,在第 770 行之类的,第五个参数$params, 被赋予了mail(),在安全模式下运行时不支持。我只是将其注释掉,瞧,它起作用了:
$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header/*, $params*/);
It's within the MailSend-function of PHPMailer.
它在MailSendPHPMailer的-function 内。
回答by Jacta
I just had this problem and found in my apache error log that sendmail was'nt installed, after installation it was all working as it should!
我刚刚遇到了这个问题,并在我的 apache 错误日志中发现 sendmail 没有安装,安装后一切正常!
root@web1:~$ tail /var/log/apache2/error.log
sh: 1: /usr/sbin/sendmail: not found
回答by slawkens
Had same problem. Just did a quick look up apache2 error.logfile and it said exactly what was the problem:
有同样的问题。刚刚快速查找了 apache2error.log文件,它准确地说明了问题所在:
> sh: /usr/sbin/sendmail: Permission denied
So, the solution was to give proper permissions for /usr/sbin/sendmailfile (it wasn't accessible from php).
因此,解决方案是为/usr/sbin/sendmail文件提供适当的权限(无法从 php 访问)。
Command to do this would be:
执行此操作的命令是:
> chmod 777 /usr/sbin/sendmail
be sure that it even exists!
确保它甚至存在!
回答by Mukesh Chapagain
Try using SMTPto send email:-
尝试使用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';
回答by Sarfraz
Make sure that you also include smtp class which comes with phpmailer:
确保您还包含 phpmailer 附带的 smtp 类:
// for mailing
require("phpmailer/class.phpmailer.php");
require("phpmailer/class.smtp.php");
回答by Greg A
To revisit an old thread, my issue was that one of the "AddressTo" email addresses was not valid. Removing that email address removed the error.
要重新访问旧线程,我的问题是“AddressTo”电子邮件地址之一无效。删除该电子邮件地址消除了错误。
回答by Shuja Ahmed
Just revisiting the old thread you can debug PHPMailer in depth by adding:
只需重新访问旧线程,您就可以通过添加以下内容来深入调试 PHPMailer:
print_r(error_get_last());
this will print out the exact error for you which is causing the default php mail() to break.
这将为您打印出导致默认 php mail() 中断的确切错误。
Hope it helps somebody.
希望它可以帮助某人。
回答by Fanky
As noted here, "This means that your PHP installation is not configured to call the mail() function correctly (e.g. sendmail_path is not set correctly in your php.ini), or you have no local mail server installed and configured."
如前所述这里,“这意味着,你的PHP安装没有配置正确地调用mail()函数(如sendmail_path没有在你的php.ini设置正确),或者你有没有本地邮件服务器安装和配置。”
In my case I had to allow the mail() function ("activate the mail() queue") in the settings of my webhost.
就我而言,我必须在我的虚拟主机的设置中允许 mail() 函数(“激活 mail() 队列”)。
回答by Kieran
Try with an address that is not gmail. They do not allow (as far as i know) smpt access to send mail from. I was doing a simple mail program last week and they also dont use default ports to send from and require that you transport across https
尝试使用不是 gmail 的地址。他们不允许(据我所知)smpt 访问来发送邮件。上周我正在做一个简单的邮件程序,他们也不使用默认端口发送并要求您跨 https 传输

