无法实例化邮件功能。发生此错误的原因

2022-08-30 19:26:06

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

答案 1

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

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


答案 2

我用了这行代码

if($phpMailer->Send()){

    echo 'Sent.<br/>';

}else{

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

}

找出问题所在。事实证明,我在安全模式下运行,并且在第 770 行或类似行中,给出了 第五个参数 ,在安全模式下运行时不受支持。我只是简单地把它注释掉了,瞧,它的工作原理:$paramsmail()

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

它在PHPMailer的-函数中。MailSend


推荐