Mohd Atiq
Mohd Atiq

Reputation: 151

How to send multiple mails with dynamically generating pdfs attachment in codeigniter using php?

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.

Upvotes: 0

Views: 803

Answers (2)

Mohd Atiq
Mohd Atiq

Reputation: 151

public function multisalarySlippdf_mail () {
    $this->load->helper('form');
    $month = $this->input->post('month');
    $year = $this->input->post('year');
    $employee_id = $this->input->post('employee_id');
    $department = $this->input->post('department');
    $wihoutemail = $this->input->post('wihoutemail');
    $this->load->model('Report_model', 'report');


    //new modify code...
    if ($month > 9) {
        $e_Setupdate = $year . '-' . $month . '-01';
    }
    else {
        $e_Setupdate = $year . '-0' . $month . '-01';
    }
    // end here ............

    if (!empty($employee_id)) {

        $salaries = $this->report->find_oneemployee_ids($employee_id);
    }
    else {

        $salaries = $this->report->find_employee_id($department);
    }


    foreach ($salaries as $sal) {

        if (!empty($wihoutemail)) {

            if ($sal->official_email <> '' OR $sal->personal_email <> '') {
                goto end;
            }
        }

        $mon_dat = $this->report->find_all_monhtlydata($sal->employee_id, $month, $year);

        $n_data = $this->report->find_pay_ledger_data_oneemployee($sal->employee_id, $month, $year);
        $m_data = $this->report->find_workingdays($sal->employee_id, $month, $year);
        $esetup_data = $this->report->find_basicpay($sal->employee_id, $e_Setupdate);

        if (!empty($esetup_data)) {

            foreach ($esetup_data as $setup) {


                if ($setup->code == 'BSCRT') {
                    $basicpay = $setup->amount;
                }
            }
        }


        switch ($month) {
            case "1":
                $month_txt = "Jan" . '-' . $year;
                break;
            case "2":
                $month_txt = "Feb" . '-' . $year;
                break;
            case "3":
                $month_txt = "Mar" . '-' . $year;
                break;
            case "4":
                $month_txt = "Apr" . '-' . $year;
                break;
            case "5":
                $month_txt = "May" . '-' . $year;
                break;
            case "6":
                $month_txt = "jun" . '-' . $year;
                break;
            case "7":
                $month_txt = "Jul" . '-' . $year;
                break;
            case "8":
                $month_txt = "Aug" . '-' . $year;
                break;
            case "9":
                $month_txt = "Sep" . '-' . $year;
                break;
            case "10":
                $month_txt = "Oct" . '-' . $year;
                break;
            case "11":
                $month_txt = "Nov" . '-' . $year;
                break;
            case "12":
                $month_txt = "Dec" . '-' . $year;
                break;
            default:
                "";

        }

        $d = [];
        $k = 0;
        $negative = $positive = [];
        foreach ($n_data as $data) {

            $d[0] = $data->code;
            $d[1] = $data->amount;
            if ($d[1] >= 0) {
                array_push($positive, $d);
            }
            else if ($d[1] < 0) {
                array_push($negative, $d);
            }
            $k++;
        }
        if (!empty($m_data)) {
            foreach ($m_data as $mdata) {
                $leave_days = $mdata->leave_days;
                $working_days = $mdata->working_days;
                $present = $mdata->present_days;
                $absent = $mdata->absent_days;
                $extraday = $mdata->extradays_inholiday;
                $additionalday = $mdata->additional_days;
                $weekoff = $mdata->week_off;


                $dojDaysDiff = 0;
                $this->load->model('Income_model', 'income');
                $doj = $this->income->find_employementdetails($sal->employee_id);
                $dojDays = date_diff(date_create($doj->joining_date), date_create($e_Setupdate));
                if ($doj->joining_date > $e_Setupdate) {
                    $dojDaysDiff = $dojDays->format("%a");
                }


            }

        }
        else {
            $leave_days = 0;
            $working_days = 0;
            $present = 0;
            $absent = 0;
            $extraday = 0;
            $additionalday = 0;
            $weekoff = 0;
        }
        $max = max(count($positive), count($negative));
        $grossamount = 0;
        $grossdeduction = 0;
        for ($i = 0; $i <= $max; $i++) {

            if (isset($positive[$i])) {
                $grossamount += $positive[$i][1];
            }

            if (isset($negative[$i])) {
                $grossdeduction += $negative[$i][1];
            }


            $data1 = array(
                'salaries'       => $sal,
                'positive'       => $positive,
                'negative'       => $negative,
                'max'            => $max,
                'month'          => $month_txt,
                'grossamount'    => $grossamount,
                'grossdeduction' => $grossdeduction,
                'leaves'         => $leave_days,
                'workingDays'    => $working_days,
                'present'        => $present,
                'absent'         => $absent,
                'extraday'       => $extraday,
                'basicpay'       => $basicpay,
                'additionalday'  => $additionalday,
                'weekoff'        => $weekoff,
                'dojDaysDiff'    => $dojDaysDiff
            );
            $html = $this->load->view('admin/salarySlippdf', $data1, true);

        }


        $file = '';
        $file = 'Salary-Slip' . $sal->employee_id;
        $pdfFilePath = "";
        $pdfFilePath = FCPATH . "uploads/" . $file . ".pdf";

        $this->load->library('m_pdf'); // load MPDF library

        $mpdf = new mPDF();
        $mpdf->WriteHTML($html);
        $mpdf->Output($pdfFilePath, "F");
        //$this->m_pdf->pdf->WriteHTML($html);


        //$this->m_pdf->pdf->Output($pdfFilePath, "F");
        //$this->m_pdf->pdf->Output($pdfFilePath, "D");

        $emailid = $sal->official_email;

        if (!empty($emailid)) {

            $config = Array(
                'protocol'  => 'smtp',
                'smtp_host' => 'ssl://smtp.googlemail.com',
                'smtp_port' => 465,
                'smtp_user' => '[email protected]',
                'smtp_pass' => 'pccpl55@Atiq07',
                'mailtype'  => 'html',
                'charset'   => 'iso-8859-1',
                'wordwrap'  => TRUE
            );
            $subject = " Salary Slip";
            $message = $html;//"Please Download file";

            $this->load->library('email', $config);

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

            //$this->email->attach($this->m_pdf->pdf->WriteHTML($html));                

            if ($this->email->send()) {
                echo 'Email send.';
                $this->email->clear(true);

            }
            else {
                show_error($this->email->print_debugger());
            }
        }
        end:
        unlink($pdfFilePath);

    }
    $this->m_pdf->pdf->Output($pdfFilePath, "D");

    return redirect('admin/report/salaryslipmail');

}

This is complete code for generating salary slip in pdf dynamically and also receiving salary slip on mail. Working perfectly for me. :)

Upvotes: 0

Denis Kuratovich
Denis Kuratovich

Reputation: 287

You need to uncomment:

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

This method clear email data and attachments form prev email. So, it'll fix problem with 1,2,3,4.... attachements, also I think it'll fix problem with same file - because files have same path.

From docs:

Initializes all the email variables to an empty state. This method is intended for use if you run the email sending method in a loop, permitting the data to be reset between cycles.

Also, it's bad paractice to load library in the cycle, so better place '$this->load->library('email', $config)' after '$config' defining

Upvotes: 1

Related Questions