【问题标题】:Could not instantiate mail function. Why this error occurring无法实例化邮件功能。为什么会出现这个错误
【发布时间】:2010-12-29 00:20:26
【问题描述】:

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

【问题讨论】:

  • 你得到什么错误信息?
  • 无法实例化邮件功能@Pekka
  • @Rajasekar - 这听起来不像是真正的错误消息,因为无法实例化函数。你能复制准确的错误输出吗?
  • 邮件发送错误 - 无法实例化邮件功能
  • 在 IIS 中,php 不使用二进制邮件程序,因此它依赖于安装的 MTA(即您需要以 SMTP 服务器为目标)。

标签: php html email phpmailer


【解决方案1】:

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

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

【讨论】:

  • sudo apt-get install sendmail 似乎在 14.04.4 中可以解决问题
【解决方案2】:

我用了这行代码

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

【讨论】:

    【解决方案3】:

    我刚遇到这个问题,在我的 apache 错误日志中发现 sendmail 没有安装,安装后一切正常!

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

    【讨论】:

      【解决方案4】:

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

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

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

      执行此操作的命令是:

      > chmod 777 /usr/sbin/sendmail
      

      确保它甚至存在!

      【讨论】:

      • 您确定要授予全世界对 sendmail 的可写访问权限吗?我认为这可能是一个漏洞。也许尝试只授予可执行访问权限:chmod +x /usr/sbin/sendmail
      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        尝试使用 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】:

          要重新访问旧线程,我的问题是“AddressTo”电子邮件地址之一无效。删除该电子邮件地址即可消除错误。

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:

              与您的房东核实,看看他们是否有每小时发送电子邮件的限制。

              【讨论】:

              • 是的,请检查您的错误日志:sendmail:550 由于违反政策而未接受电子邮件:已达到收件人限制,xxx >= xxx 每天。
              【解决方案10】:

              这是一个系统错误。

              检查系统错误:

              tail /var/log/httpd/error_log
              

              可以是任何原因。

              【讨论】:

                【解决方案11】:

                只需重新访问旧线程,您就可以通过添加来深入调试 PHPMailer:

                print_r(error_get_last());
                

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

                希望对某人有所帮助。

                【讨论】:

                • 这对我有帮助!这是来自print_r 的完整错误:Array ( [type] =&gt; 2 [message] =&gt; 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] =&gt; ...\vendor\phpmailer\phpmailer\src\PHPMailer.php [line] =&gt; 852 )
                • 所以您的 SMTP 配置不正确。如错误所示,您可以通过在 php.ini 中提及端口、主机、用户名和其他凭据或通过 ini_set() 内联指定它们来配置 SMTP 服务器设置。我建议阅读 SMTP 文档以正确设置它。一点谷歌搜索肯定会有所帮助!
                • 谢谢 我的印象是 PHPMailer 可以自己处理 SMTP。我正在使用我的 gmail smtp 凭据,现在可以使用了。
                【解决方案12】:

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

                在我的情况下,我必须在我的虚拟主机的设置中允许 mail() 功能(“激活 mail() 队列”)。

                【讨论】:

                  【解决方案13】:

                  “无法实例化邮件函数”是 PHPMailer 报告对 mail() 的调用(在邮件扩展中)失败的方式。 (所以您使用的是“邮件”邮件程序。)

                  您可以尝试在 PHPMailer::MailSend 中调用 mail() 之前删除 @s,然后查看是否有错误被静默丢弃。

                  【讨论】:

                    【解决方案14】:

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

                        $mail->IsSMTP(); 
                    
                        $mail->Host='hote_smtp'; 
                    

                    将 hot_smtp 更改为正确的值

                    【讨论】:

                      【解决方案15】:

                      一个旧线程,但它可能会帮助像我这样的人。 我通过在 PHP.ini 中将 SMTP 服务器值设置为合法值解决了这个问题

                      【讨论】:

                        【解决方案16】:

                        我有同样的错误。回复是导致问题的原因。我删除了它。

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

                        【讨论】:

                          【解决方案17】:

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

                          getsebool httpd_can_sendmail
                          

                          您可以通过调用下面的命令来启用它。 -P 参数使其永久化。

                          setsebool -P httpd_can_sendmail 1
                          

                          【讨论】:

                            猜你喜欢
                            • 2015-08-19
                            • 1970-01-01
                            • 1970-01-01
                            • 2017-09-07
                            • 2019-07-24
                            • 2011-08-08
                            • 1970-01-01
                            • 2017-10-04
                            • 2015-01-17
                            相关资源
                            最近更新 更多