2012-05-09 7 views
0

私は、顧客が私たちのサイトから何かを購入したときにPDFレシートを作成するスクリプトを作成しました。 PDFを作成したら、出力を変数に保存します。ob_get_clean()電子メールにbase64_encoded文字列を付ける - Codeigniter

この変数をbase64_encoded文字列にします。私がそれをやったら、その文字列をデータベースに保存します。これでやりたいことは、文字列を取得して電子メールの添付ファイルとして保存して、ファイルとしてダウンロードできるようにすることです。私はGoogleを試しましたが、本当に役立つものは何も見つかりませんでした。

このスレッドは見つかりましたが、Codeigniterの電子メールライブラリ(私が見逃している可能性があります)に表示されている限り、要求された機能は実装されていませんでした。ここでは、独自のライブラリを作成して、PHPのメール機能と適切なヘッダを使用して電子メールを送信することができますけれども、要求、事前にEmail class: add attachment from string

おかげで、 /名無し

+0

はではSwiftMailer、phpmailerのように、あなたのために、添付ファイル、HTML形式のメールなどを扱うサードパーティの電子メールのライブラリがあります。 – bumperbox

答えて

2

です。私たちは、引数としてMIMEタイプを渡すときのCodeIgniterのEmailクラスで

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

は、次のコードが実行されます。

$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)); 

にコードを変更するよう

今アタッチメント配列はよう:私は電子メールをintialzedとき

$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' 
    ); 

今:

$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

関連する問題