Search code examples
phpcodeigniterphpmailer

Sent mail with attachment in Codeigniter to multiple list


With below code its work fine. $maillist is my list in with member with email. Now problem when i run this. member receive multiple attachment as list move forward.

like my second member receive two attachment[same attachment] third receice three and so on.

Your help will really appreciated. Thanks in advance Prashant.

$path_to_the_file = "/data/mail-attachment/";
                $attachfile = $path_to_the_file.$mailinfo->attachment;

                //SET COMMON VARIABLES
                $fromname = "[email protected]";
                $subjectline = $mailinfo->subjectline;
                $this->load->library('email');
                $message = $mailinfo->mailbody;
                $message .= "<br /><br /><a href=\"$attachfile\">Download Attachment</a>";

                foreach ($maillist as $key => $value) 
                {
                    $emailto = $value->email;

                    $this->email->set_newline("\r\n");
                    $this->email->from($fromname);      // change it to yours
                    $this->email->to($emailto);         // change it to yours
                    $this->email->subject($subjectline);
                    $this->email->message($message);
                    $this->email->attach($attachfile);
                    $this->email->set_mailtype("html");

                    if($this->email->send()) { 
                        $success_mail_count++;
                    }       
                }

Solution

  • Try This..... You meed to pass clear(TRUE) to clear attached file after mail send.

            path_to_the_file = "/data/mail-attachment/";
            $attachfile = $path_to_the_file . $mailinfo->attachment;
    
            //SET COMMON VARIABLES
            $fromname = "[email protected]";
            $subjectline = $mailinfo->subjectline;
            $this->load->library('email');
            $message = $mailinfo->mailbody;
            $message .= "<br /><br /><a href=\"$attachfile\">Download Attachment</a>";
            $emailto = array();
            foreach ($maillist as $key => $value) {
                $emailto = $value->email;
            }
            $this->email->set_newline("\r\n");
            $this->email->from($fromname);   // change it to yours
            $this->email->to($emailto);   // change it to yours
            $this->email->subject($subjectline);
            $this->email->message($message);
            $this->email->attach($attachfile);
            $this->email->set_mailtype("html");
            if ($this->email->send()) {
                $this->email->clear(TRUE);
                $success_mail_count++;
            }