Codeigniter:在循环发送多封电子邮件时,未清除上次电子邮件的电子邮件附件

时间:2022-10-23 17:07:26

My code sends multiple emails in loop with attachment,

我的代码在附件中循环发送多封电子邮件,

Problem is attachments of last(previous all) emails get attached to next email.

问题是最后(以前所有)电子邮件的附件附加到下一封电子邮件。

ex. suppose 3 emails in database with 1 attachment in each(a1.pdf, a2.pdf, a3.pdf) then, it sends email with attachment as

恩。假设3个电子邮件在数据库中,每个电子邮件中有1个附件(a1.pdf,a2.pdf,a3.pdf)然后,它会发送带有附件的电子邮件

email 1:

attachment :a1.pdf

email 2:

attachment :a1.pdf, a2.pdf

附件:a1.pdf,a2.pdf

email 3:

attachment :a1.pdf, a2.pdf, a3.pdf

附件:a1.pdf,a2.pdf,a3.pdf

I am using codeigniter framework.

我正在使用codeigniter框架。

My code is (this code is called in loop)

我的代码是(这个代码在循环中调用)

. . .

。 。 。

$this->email->subject($item->subject);

        $this->email->message($message);
        $attachments='';
        if(strlen($item->attachment) > 5)
        {
            $attachments = explode(',', $item->attachment);
            foreach($attachments as $attachment)
            {
                if(strlen($attachment)>5)
                $this->email->attach(FCPATH . 'attachments/' . $attachment);                    
            }                

        }

      $this->email->send();

. . .

。 。 。

2 个解决方案

#1


14  

You need to use $this->email->clear(); to clean out the variables set within the loop. Read the manual.

你需要使用$ this-> email-> clear();清除循环中设置的变量。阅读手册。

#2


17  

You need to reset it in CodeIgniter.

您需要在CodeIgniter中重置它。

At the end of the loop add:

在循环结束时添加:

$this->email->clear(TRUE);

This resets all email variables including the attachments, allowing you to create a new mail.

这会重置所有电子邮件变量,包括附件,允许您创建新邮件。

#1


14  

You need to use $this->email->clear(); to clean out the variables set within the loop. Read the manual.

你需要使用$ this-> email-> clear();清除循环中设置的变量。阅读手册。

#2


17  

You need to reset it in CodeIgniter.

您需要在CodeIgniter中重置它。

At the end of the loop add:

在循环结束时添加:

$this->email->clear(TRUE);

This resets all email variables including the attachments, allowing you to create a new mail.

这会重置所有电子邮件变量,包括附件,允许您创建新邮件。