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

时间:2012-03-22 05:59:28

标签: php phpmailer

我正在尝试向我的数据库中的多个电子邮件地址发送电子邮件。这是我当前的代码。我需要让他们查询我的数据库并将电子邮件发送到每个电子邮件地址。它正在工作,但电子邮件只发送到第一个电子邮件地址,并收到错误“致命错误:调用未定义的方法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'; }
    }
}

2 个答案:

答案 0 :(得分:1)

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/');

答案 1 :(得分:0)

我猜你正在使用SMTP,因为我看不到$mail的来源。

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

之后,它未被定义为PHPMailer对象,因此当您执行对象分配stdClass时,它将被转换为$mail->From = $head_mid

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