2013-03-28 165 views
2

我使用TCPDF即時生成pdf文件。通過使用TCPDF我得到原始文件與base64編碼現在我想發送這個原始數據作爲電子郵件附件使用codeigniter電子郵件幫助功能。codeigniter發送PDF文件作爲電子郵件附件

這怎麼辦?

+0

您需要將其保存爲服務器上的文件,然後將其附加。 –

+0

有沒有辦法在服務器上保存文件? –

+0

是的,你必須擴展電子郵件庫並改變一些方法(特別是'_build_message()')來接受原始文件內容而不是文件路徑。通過圖書館看看,你會看到它如何處理附件。擴展並改變以適應您的需求。 –

回答

0

你要保存文件與電子郵件

 $obj = new $this->pdf; 
     $obj->SetSubject('BLAH BLAH'); // set document information 
     $obj->SetKeywords('Blah, Blah, Blah'); 
     $obj->AddPage(); // add a page 
     $obj->SetFont('helvetica', '', 6); 
     $obj->writeHTML("Your text goes here", true, false, false, false, ''); 
     $obj->Output('Path/to/save/filename.pdf', 'F'); //Close and output PDF document 

而且會比安裝前發送電子郵件這樣

 $this->load->library('email'); 
    $this->email->from('[email protected]', 'Example'); 
    $this->email->to('[email protected]'); 
    $this->email->subject('Subject Goes Here'); 
    $this->email->message('Message goes here'); 
    $this->email->attach('Path/to/saved/filename.pdf'); 
    $this->email->send(); 
+0

我知道這一點,但我想在我的服務器上發送保存pdf的電子郵件。 –

2
/*its batter to make function*/ 

/*-------COPY THIS CODE---- */ 

    $newFile = 'Path/to/save/filename.pdf'; 
    $obj = new $this->pdf; 
    $obj->SetSubject('BLAH BLAH'); // set document information 
    $obj->SetKeywords('Blah, Blah, Blah'); 
    $obj->AddPage(); // add a page 
    $obj->SetFont('helvetica', '', 6); 
    $obj->writeHTML("Your text goes here", true, false, false, false, ''); 
    $obj->Output($newFile, 'F'); //Close and output PDF document 

/*-------SENDING EMAIL---- */ 

$this->load->library('email'); 
$this->email->from('[email protected]', 'Example'); 
$this->email->to('[email protected]'); 
$this->email->subject('Subject Goes Here'); 
$this->email->message('Message goes here'); 
$this->email->attach($newFile); 
$this->email->send(); 
-1
$newFile = 'Path/to/save/filename.pdf'; 
    $obj = new $this->pdf; 
    $obj->SetSubject('BLAH BLAH'); // set document information 
    $obj->SetKeywords('Blah, Blah, Blah'); 
    $obj->AddPage(); // add a page 
    $obj->SetFont('helvetica', '', 6); 
    $obj->writeHTML("Your text goes here", true, false, false, false, ''); 
    $obj->Output($newFile, 'F'); //Close and output PDF document 
3

拿了我一會兒找答案,希望對未來的人有所幫助:

// Get email address and base64 via ajax 
    $email = $this->input->post('email'); 
    $base64 = $this->input->post('base64'); 

    $base64 = str_replace('data:application/pdf;base64,', '', $base64); 
    $base64 = str_replace(' ', '+', $base64); 

    $data = base64_decode($base64); 

    // Locally emails do now work, so I use this to connect through my gmail account to send emails 
    $config = Array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.googlemail.com', 
     'smtp_port' => 465, 
     'smtp_user' => '[email protected]', 
     'smtp_pass' => 'MyEmailPassword', 
     'mailtype' => 'html', 
     'charset' => 'iso-8859-1' 
    ); 

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

    $this->email->set_newline("\r\n"); 
    $this->email->to($email); 
    $this->email->from('[email protected]', 'base64'); 
    $this->email->subject('base64'); 
    // Using the string_attach($str_file, $filename, $mime, $disposition = 'attachment') 
    // function located in CodeIgniter\application\libraries\Email.php 
    $this->email->string_attach($data, 'base64.pdf', 'application/pdf'); 

    $this->email->send(); 
相關問題