phpmailer错误"无法实例化邮件功能"

sdf*_*for 33 php email phpmailer

我正在使用mail()稍微修改我的用户ID 的基本示例,我收到错误"Mailer Error:无法实例化邮件功能"

如果我使用邮件功能 -

mail($to, $subject, $message, $headers);
Run Code Online (Sandbox Code Playgroud)

它工作正常,虽然我在发送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@domain.com","my name");
    $mail->SetFrom('from@domain.com', 'my name');
    $address = "to@domain.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!";
    }
?>
Run Code Online (Sandbox Code Playgroud)

Muk*_*ain 26

尝试使用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';
Run Code Online (Sandbox Code Playgroud)

  • SMTP的问题是它需要在本地安装SMTP服务器...除非我弄错了. (3认同)
  • 截至 2018 年,该函数称为“$mail-&gt;isSMTP()”。还是救了我,谢谢! (2认同)

Avi*_*ash 13

$mail->AddAddress($address, "her name");
Run Code Online (Sandbox Code Playgroud)

应改为

$mail->AddAddress($address);
Run Code Online (Sandbox Code Playgroud)

这适用于我的情况..

  • 名字有什么问题吗? (2认同)

小智 10

您需要确保您的发件人地址是该服务器上的有效电子邮件帐户设置.


Mat*_*epa 10

这对我有用

$mail->SetFrom("from@domain.co","my name", 0); //notice the third parameter
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!对于任何想知道原因的人,这里解释了第三个参数:http://develop.wp-a2z.org/oik_api/phpmailersetfrom/ (2认同)

Sal*_*n A 9

如果您要发送文件附件,并且您的代码适用于小附件但大型附件失败:

如果您在尝试发送大型电子邮件时收到错误"无法实例化邮件功能"错误,并且您的PHP错误日志包含消息"无法发送邮件:太大",那么您的邮件传输代理(sendmail,postfix,exim等)拒绝提供这些电子邮件.

解决方案是配置MTA以允许更大的附件.但这并不总是可行的.备用解决方案是使用SMTP.您需要访问SMTP服务器(如果您的SMTP服务器需要身份验证,则需要登录凭据):

$mail             = new PHPMailer();
$mail->IsSMTP();                           // telling the class to use SMTP
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // set the SMTP server
$mail->Port       = 26;                    // set the SMTP port
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password
Run Code Online (Sandbox Code Playgroud)

PHPMailer默认使用PHP mail()函数,该函数使用php.ini通常默认使用sendmail(或类似的东西)的设置.在上面的示例中,我们覆盖了默认行为.


Irw*_*uin 7

您的代码看起来不错,您是否忘记在服务器上安装PostFix?

sudo apt-get install postfix
Run Code Online (Sandbox Code Playgroud)

它对我有用;)

干杯

  • 也为我工作,也在他们的文档中:https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#could-not-instantiate-mail-function (2认同)

Mat*_*ope 5

关于此特定错误的PHPMailer 帮助文档帮助我走上了正确的道路。

我们发现 php.ini 没有定义 sendmail_path,所以我添加了sendmail_path = /usr/sbin/sendmail -t -i;