如何使用PHP在Codeigniter中发送动态生成pdf附件的多封邮件?

时间:2018-08-29 04:45:21

标签: php mysql wordpress codeigniter

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 29 Aug 2018 08:12:31 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.3.3
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Powered-By: PleskLin
X-Robots-Tag: noindex,nofollow

我的代码发送邮件完美,但是附件存在问题。在邮件附件中,一个员工收到了工资单,但第二名员工也通过一封邮件两次收到了第一名员工(相同)的附件,第三名员工也收到了三次相同的附件。我不知道代码中的问题在哪里。 我对Php一点了解。 请指导我。 预先感谢。

2 个答案:

答案 0 :(得分:1)

您需要取消注释:

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

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

来自文档:

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

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

答案 1 :(得分: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' => 'pccplatiq07@gmail.com',
                '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('mohd.atiq07@gmail.com');
            $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中动态生成薪水单,并通过邮件接收薪水单。非常适合我。 :)