CodeIgniter Forums
How to send multiple attachments in mail at the time of generating pdf in codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How to send multiple attachments in mail at the time of generating pdf in codeigniter (/showthread.php?tid=71558)



How to send multiple attachments in mail at the time of generating pdf in codeigniter - ArshKhan - 08-28-2018

Code:
public function multisalarySlippdf_mail(){

$file = 'Salary Slip';
$pdfFilePath1 = FCPATH . "uploads/".$file.".pdf";
if(file_exists($pdfFilePath1)){
  unlink($pdfFilePath1);
                       }
$this->load->library('m_pdf');// load mpdf library
$config = Array(
       'protocol' => 'smtp',
       'smtp_host' => 'ssl://smtp.googlemail.com',
       'smtp_port' => 465,
       'smtp_user' => '[email protected]',
       'smtp_pass' => 'pccpl525',
       'charset' => 'iso-8859-1',
       'mailtype' => 'html',
       'charset' => 'utf-8',
       'newline' => '\r\n',
       'crlf'  => '\n',
       'wordwrap' => TRUE

                       );
$salaries  = $this->report->fetchAlldata();// fetching salary data from
                                          database using report model

foreach($salarie as $sal){
$emailid = $sal->official_email; // fetch email ids from the database
if(!empty($emailid)){
$html = $this->load->view('admin/salarySlippdf',
['salaries'=>$sal,
'workingDays'=>$working_days,
'present'=>$present,
'absent'=>$absent,'extraday'=>$extraday,],true);
}
$this->m_pdf->pdf->WriteHTML($html);
$this->m_pdf->pdf->Output($pdfFilePath1, "F");// saved file in given path
$subject = " Salary Slip";
$message = "Please Download file".' '.$emailid;
$this->load->library('email', $config);
//$this->email->clear(TRUE);
$this->email->set_newline("\r\n");
$this->email->from('[email protected]');
$this->email->to($emailid);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach($pdfFilePath1);
if($this->email->send()){
  echo "send";
}
else{
show_error($this->email->print_debugger());
}unlink($pdfFilePath1);
}
}
return redirect('admin/report/salaryslipmail');
}

My code is sending mail perfectly but there is problem in attachment. In mail attachment one employee received his salary slip but second employees also received the first employee attachment(same) two times in one mail and third employee also received the same attachment three times. I don't know where is the problem in my code. I have little bit knowledge of Php. Please guide me. Thanks in advance.


RE: How to send multiple attachments in mail at the time of generating pdf in codeigniter - Pertti - 08-29-2018

It's probably when you were doing debug.

$this->email->print_debugger() only works if you don't clear email after send with $this->email->send(false). The problem with that is, it'll keep all attachments in place too.

If you uncomment $this->email->clear(TRUE) - that will clear all the previous email data, and with true attribute, it will also clear attachments.