Codeigniter: Email attachment of last emails not cleared while sending multiple emails in loop

你。 提交于 2019-12-08 15:04:35

问题


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

email 1:

attachment :a1.pdf

email 2:

attachment :a1.pdf, a2.pdf

email 3:

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

I am using codeigniter framework.

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();

. . .


回答1:


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




回答2:


You need to reset it in 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.



来源:https://stackoverflow.com/questions/7050690/codeigniter-email-attachment-of-last-emails-not-cleared-while-sending-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!