Phpmailer 错误“无法实例化邮件函数”

2022-08-30 10:36:28

我正在使用对我的用户ID稍作修改的基本示例,并且收到错误“Mailer错误:无法实例化邮件功能”mail()

如果我使用邮件功能 -

mail($to, $subject, $message, $headers);

它工作正常,尽管我在发送HTML时遇到问题,这就是为什么我尝试PHPMailer的原因。

这是代码:

<?php
require_once('../class.phpmailer.php');

    $mail             = new PHPMailer(); // defaults to using php "mail()"
    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[\]",'',$body);
        print ($body ); // to verify that I got the html
    $mail->AddReplyTo("reply@example.com","my name");
    $mail->SetFrom('from@example.com', 'my name');
    $address = "to@example.com";
    $mail->AddAddress($address, "her name");
    $mail->Subject    = "PHPMailer Test Subject via mail(), basic";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
    $mail->MsgHTML($body);
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
?>

答案 1

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

你的代码看起来不错,你忘了在服务器上安装PostFix吗?

sudo apt-get install postfix

它对我有用;)

干杯


推荐