在Codeigniter中发送电子邮件时如何下载和附加文件?

时间:2018-08-02 06:26:45

标签: php codeigniter

enter image description here

-我想单击下载按钮,然后以pdf格式下载此帐单,也希望将此pdf格式附加到电子邮件中。

我使用此代码-

public function details_pdf($id) {
    $this->load->model('user_model');
    $this->load->helper('download');

    $data['invoice_detls'] = $this->user_model->front_get_invoice_details(['front_orders_id'=>$id]);
    $data['items'] = $this->user_model->front_get_ordered_items(['front_dish_invoice_id' => $id]);

    //$data['abstract_message'] = $this->user_model->get_abstract_message(['abstract_id' => $id]);
    //$data['user_details'] = $this->user_model->get_user(['id' => $data['abstract_message'][0]['user_id']]);

    //$data['user_details_member'] = $this->user_model->get_profile_details(['members.userid' => $data['abstract_message'][0]['user_id']]);
    //print_r($data['user_details']);die();

    $customer_name =  $data['invoice_detls'][0]['customer_name'];
    $customer_phone =  $data['invoice_detls'][0]['customer_phone'];

    //load the view and saved it into $html variable
    $html = $this->load->view('front_billing_view', $data, true);
    $time = time();


    //this the the PDF filename that user will get to download
    $pdfFilePath = "$customer_name.'_'.$customer_phone.'_'.$time.pdf";

    $this->user_model->update_order(['front_orders_id' => $id],['bill_pdf'=>$pdfFilePath]);

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

    //generate the PDF from the given html
    $this->m_pdf->pdf->WriteHTML($html);

    //download it.
    $this->m_pdf->pdf->Output($pdfFilePath, "D");

    /*--------------------Mail function Start-----------------------*/
        $order_fetch = $this->user_model->front_get_invoice_details(['front_orders_id' => $id]);

        $email = $order_fetch[0]['customer_email'];
        $pdf_name = $order_fetch[0]['bill_pdf'];
            $config = Array(
               'protocol' => 'smtp',
               'smtp_host' => 'ssl://smtp.googlemail.com',
               'smtp_port' => 465,
               'smtp_user' => 'noreply@wsdev.in',
               'smtp_pass' => '%KidFlash!2#4%',
               'mailtype' => 'text/html',
               'charset' => 'iso-8859-1',
               'charset' => 'utf-8'
            );
            $this->load->library('email');
            $this->email->set_mailtype("html");
            $this->email->set_newline("\r\n");
            $from_email = 'noreply@wsdev.in';
            $this->email->from($from_email);
            $this->email->to($email);
            $this->email->subject('Order Delivered');
            $this->email->message('Your Order Successfully Delivered.');                       
            $this->email->attach($pdfFilePath);
            $this->email->send();

    /*--------------------Mail Function End-----------------------*/ 


}

此代码仅下载pdf,但由于此文件存储在桌面而不是项目文件夹中,因此我无法将下载文件作为电子邮件发送给单击下载按钮的电子邮件。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

attach()方法需要完整的路径/名称,例如。 `/home/user/file.pdf'-Codeigniter Email Class

在生成带有下载选项的pdf文档之前,您可以先在本地生成pdf,然后再使用dowload选项生成pdf。

//generate the PDF from the given html
$this->m_pdf->pdf->WriteHTML($html);

// save it 
$tmpPdfFile = '/home/user/file.pdf'; // change this acording to your configuration
$mpdf->Output($tmpPdfFile,'F');

//download it.
$this->m_pdf->pdf->Output($pdfFilePath, "D");


// attach file to mail
 $this->email->attach($tmpPdfFile);
 $this->email->send();

// delete $tmpPdfFile after email send if you don't need it
unlink($tmpPdfFile);