生成pdf并直接发送给邮件codeigniter

时间:2016-04-29 23:26:15

标签: php codeigniter-3

我使用codeiegnter和dompfd库编写代码以生成pdf文件我生成文件没有任何问题但我需要在发送邮件时将其发送为atachment whit succus。

但我尝试发送电子邮件我不能

这是我的代码

$this->load->library('Pdf');
   $this->pdf->set_paper($customPaper,$paper_orientation);
   
  $this->pdf->load_view('app/pdf',$data,true);
//  $this->set_paper(array(0,0,950,950),'landscape');
  $this->pdf->render();
  $this->pdf->stream($id.".pdf");  


 $this->email->initialize(array(
   'protocol' => 'smtp',
   'smtp_host' => 'xxxxx',
  'smtp_user' => 'xxxxxxx', 
      'smtp_pass' => 'xxxxxxx', 
  'smtp_port' =>25,
  'crlf' => "\r\n"

));
      
$this->email->from($from);
$this->email->to( $emaile);

$this->email->subject( $subje);
$this->email->message($msg);


  $this->email->attach(// gere I need some thins to send this pdf as atachmanet );

$this->email->send();

我怎么能这样做呢?

1 个答案:

答案 0 :(得分:1)

生成pdf并直接将代码igniter发送到邮件

第1步

包含PhpMailer

第2步

要下载TCPDF Click Here

第3步

将上述下载文件解压缩到application / libraries / tcpdf中。

第3步

在application / libraries / Pdf.php中创建一个新文件

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';
class Pdf extends TCPDF
{ function __construct() { parent::__construct(); }
}
/*Author:Tutsway.com */
/* End of file Pdf.php */
/* Location: ./application/libraries/Pdf.php */

第4步

在application / controllers / pdfexampleandemailsend.php中创建Controller文件。

        /*------------------------Generate Pdf-------------------------*/

        $this->load->library('Pdf');
        $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $obj_pdf->SetCreator(PDF_CREATOR);
        $title = "PDF Report";
        $obj_pdf->SetTitle('Hafiz Adil');
        $obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Hafiz Adil', PDF_HEADER_STRING);
        //$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        //$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $obj_pdf->SetDefaultMonospacedFont('helvetica');
        $obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        $obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $obj_pdf->SetFont('helvetica', '', 9);
        $obj_pdf->setFontSubsetting(false);
        $obj_pdf->AddPage();
        /*ob_start();
            // we can have any view part here like HTML, PHP etc
            $content = ob_get_contents();
        ob_end_clean();*/
        $obj_pdf->writeHTML('Medicine', true, false, true, false, '');
        $pdff = $obj_pdf->Output('output.pdf', 'S');

        //print_r($pdff); echo "<br>"; die('testing');

        /*------------------------Generate Pdf-------------------------*/

        //die('not error');
        $sendMessage = '<html>
                    <body bgcolor="#EDEDEE">
                    <p><strong> Dear:  '. $fname . '!</strong></p>
                     <p> Email:  '.$email.'</p>
                     <p> Message: '.$this->input->post('comments').'</p>
                     <p>Team: <strong>MVPEdge</strong><p>
                  </body>
                  </html>';

        //echo "<pre>"; print_r($sendMessage);
        $this->phpmailer->AddAddress("testing@gmail.com"); 
        $this->phpmailer->IsMail();

        $this->phpmailer->AddStringAttachment($pdff, 'output.pdf');

        $this->phpmailer->From      = $email;
        $this->phpmailer->FromName = $fname;            
        $this->phpmailer->IsHTML(true);                                  
        $this->phpmailer->Subject        =  "Contact us inquiry";
        $this->phpmailer->Body           =  $sendMessage;
        $this->phpmailer->Send();
        $this->phpmailer->ClearAddresses();
        //print_r($sendMessage);die;
        $this->session->set_flashdata('success', 'Your contact information sent successfully. You will be notify via email.');