2012-05-09 48 views
0

我已經花了最後一天創建一個腳本,當客戶從我們的網站購買東西時,該腳本將創建PDF收據。當創建PDF時,我將輸出保存到一個變量使用ob_get_clean()在電子郵件中附加base64編碼的字符串 - Codeigniter

然後,我把這個變量變成一個base64_encoded字符串。當我這樣做時,我將字符串保存到數據庫中。現在,我想要做的是獲取字符串,並以某種方式將其保存爲電子郵件的附件,以便用戶可以將其作爲文件下載。我試過谷歌,但我沒有發現真正有用的東西。

我發現了這個線程,但就我在Codeigniter電子郵件庫中看到的(我可能錯過了它)而言,所請求的函數未實現。這裏雖然要求,Email class: add attachment from string

在此先感謝, /Nanashi

+0

有跡象表明,處理附件,HTML郵件等你,像SwiftMailer,PHPMailer的第三方電子郵件庫。 – bumperbox

回答

2

您可以創建自己的庫,並使用PHP的郵件功能和適當的標題發送電子郵件。

function send_email($to, $from, $subject, $body, $attachment_string) 
{ 

$filename = "receipt.pdf"; 
$uid = md5(uniqid(time())); 
$attachment=chunk_split($attachment_string); 

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
$headers .= "From: <".$from.">\r\n"; 
$headers .= "This is a multi-part message in MIME format.\r\n"; 
$headers .= "--".$uid."\r\n"; 
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n"; 
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
$headers .= $body."\r\n\r\n"; 
$headers .= "--".$uid."\r\n"; 
$headers .= "Content-Type: application/pdf; name=\"".basename($filename)."\"\r\n"; // use different content types here 
$headers .= "Content-Transfer-Encoding: base64\r\n"; 
$headers .= "Content-Disposition: attachment; filename=\"".basename($filename)."\"\r\n\r\n"; 
$headers .= $attachment."\r\n\r\n"; 
$headers .= "--".$uid."--"; 

if(mail($to, $subject, $body, $headers)) 
{ 
    echo "success"; 
} 

} 
+0

謝謝!這會做現在。 –

0

在codeigniter Email類中,當我們將MIME類型作爲參數傳遞時,執行以下代碼。

$file_content =& $file; // buffered file 

$this->_attachments[] = array(
     'name'  => array($file, $newname), 
     'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters 
     'type'  => $mime, 
     'content' => chunk_split(base64_encode($file_content)), 
     'multipart' => 'mixed' 
    ); 

chunk_split(base64_encode($file_content))將打破我們傳遞給$this->email->attach()功能以base64文件。

讓我改變了代碼

$file_content =& $file; // buffered file 
    $file_content = ($this->_encoding == 'base64') ? $file_content : chunk_split(base64_encode($file_content)); 

現在附件數組:

$this->_attachments[] = array(
     'name'  => array($file, $newname), 
     'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters 
     'type'  => $mime, 
     'content' => $file_content, 
     'multipart' => 'mixed' 
    ); 

現在,當我intialzed電子郵件:

$config['_bit_depths'] = array('7bit', '8bit','base64'); 
    $config['_encoding'] = 'base64' 
    $this->load->library('email',$config); 

可能是我做錯誤的方式,但它的作品。

$this->email->attach($base64,'attachment','report.pdf','application/pdf');  

下載修改過的電子郵件類:

https://github.com/aqueeel/CI-Base64EmailAttach

相關問題