如何使用php在codeigniter中动态生成pdfs附件发送多封邮件?

问题描述 投票:0回答:2
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'); 
}

我的代码完美地发送邮件,但附件有问题。在邮件附件中,一名员工收到了他的工资单,但第二名员工也在一封邮件中收到了第一份员工附件(相同)两次,而第三名员工也收到了三次相同的附件。我不知道我的代码中的问题在哪里。我对Php知之甚少。请指导我。提前致谢。

php mysql wordpress codeigniter
2个回答
1
投票

您需要取消注释:

$这个 - >的电子邮件 - >清除(真);

此方法清除电子邮件数据和附件形成上一封电子邮件。因此,它将修复1,2,3,4 ....附件的问题,我认为它将解决同一文件的问题 - 因为文件具有相同的路径。

来自docs:

将所有电子邮件变量初始化为空状态。如果您在循环中运行电子邮件发送方法,允许在循环之间重置数据,则可以使用此方法。

另外,在循环中加载库是不好的做法,因此在'$ config'定义之后更好地放置'$ this-> load-> library('email',$ config)'


0
投票
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');

}

这是用于动态生成pdf工资单以及在邮件上收到工资单的完整代码。为我完美工作。 :)

© www.soinside.com 2019 - 2024. All rights reserved.