致命错误:调用PHP邮件程序中未定义的方法stdClass::AddAddress()


Fatal error: Call to undefined method stdClass::AddAddress() in PHP mailer

我正试图向数据库中的多个电子邮件地址发送电子邮件。这是我当前的代码。我需要让他们查询我的数据库,并将电子邮件发送到每个电子邮件地址。它正在工作,但电子邮件只发送到第一个电子邮件地址,并得到一个错误"致命错误:调用未定义的方法stdClass::AddAddress()"。我在哪里出错了?

<?php
$elist = $database->getRows("SELECT * FROM `emails`");
foreach($elist as $emails){
        $frm = 'test@gmail.com';
        $sub = 'Weekly Work Report';
        ob_start();
        include_once('mail_content.php');
        $mail_body = ob_get_contents(); 
        ob_end_clean();
        $to = $emails['email'];
        $mailstatus = lm_mail('1', '2', $to, '3', $frm, 'HR', $sub, $mail_body);
if ($mailstatus == 'ok') {
$response->redirect('index.php?com_route=user_report');
} else {
    echo $mailstatus;
}
}
?>
function lm_mail($head_mid='',$head_mname='',$to_mid ,$to_mname='',$reply_mid,$reply_mname='',$subject,$body,$attachments='')
{
    include_once 'phpmailer/mail_config.php';
    if(SMTP_mail)
    {
        // Send SMTP Mails
        $mail->From =$head_mid ;  // From  Mail id
        $mail->FromName = $head_mname; // From  Name
        $mail->AddAddress($to_mid,$to_mname); // To Address
        $mail->AddReplyTo($reply_mid,$reply_mname); // From Address
        $mail->Subject=$subject;
        $mail->Body =  $mail_body.$body; //HTML Body
        $mail->AltBody = "This is the body when user views in plain text format"; //Text Body
        if(!$mail->Send())
        {
            return $mail->ErrorInfo;
        }
        else
        {
           return 'ok';
        }
    }
    else
    {
        $mail  = new PHPMailer(); // defaults to using php "mail()"
        $mail->AddReplyTo($reply_mid,$reply_mname); // Sender address
        $mail->AddReplyTo($reply_mid,$reply_mname); // replay to address
        $address = $to_mid;  // to addtesas
        $mail->AddAddress($address, $to_mname);
        $mail->Subject    = $subject;
        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
        $mail->MsgHTML($mail_body.$body);
        if(!$mail->Send())
        {
            return  $mail->ErrorInfo;
        }
        else { return  'ok'; }
    }
}

lm_mail函数调用的第一个条件中,没有对象被实例化。

if(SMTP_mail)
{
    // No $mail object?
    // Send SMTP Mails
    $mail->From =$head_mid ;  // From  Mail id

尝试添加:

if(SMTP_mail)
{
   $mail = new PHPMailer();  // create a new object
   $mail->IsSMTP(); // enable SMTP
   // Have to manually set language if PHPMailer can't determine
   $mail->SetLanguage("en", 'includes/phpMailer/language/');

我猜您使用的是SMTP,因为我不知道$mail来自哪里。

由于发送了一封电子邮件,我的猜测是phpmailer/mail_config.php设置了一个$mail对象并设置了SMTP_mail常量,然后在第一次函数调用后它就超出了范围,并且该文件只包含了一次,因此不会再次定义它。

之后,它没有被定义为PHPMailer对象,因此在执行对象分配$mail->From = $head_mid时,它被强制转换为stdClass

请尝试从mail_config.php中取出代码并在您的send函数中复制它,或者向mail_config.php添加一个函数,该函数提供了一个工厂来获取根据您的需要配置的PHPMailer对象。