2011-08-11 11 views
2

として出てくる私は、base64として画像をエンコードしようとしてきた後、PHPのメールを使用して電子メールの添付ファイルとして送信された()が、私は取得しています、すべては私の電子メールでbase64でテキストです。ここに私が使用しているものがあります:PHP:メール()base64でエンコードされた画像を取り付けるには、テキスト

$boundary1 = 'bound1'; 
$boundary2 = 'bound2'; 
$to = '[email protected]'; 
$subject = 'Test Image attachment'; 
$headers = 'From: "Me" <[email protected]>'."\r\n"; 
//add boundary string and mime type specification 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary1\""; 
//define the body of the message. 
$message = 'Content-Type: image/png; name="'.$file_name.'"'."\n"; 
$message .= "Content-Transfer-Encoding: base64\n"; 
$message .= "Content-Disposition: inline; filename=\"$file_name\"\n\n"; 
$message .= base64_encode_image("signature_files/".$file_name, 'png')."\n"; 
$message .= "--" . $boundary2 . "\n"; 
//send the email 
mail($to, $subject, $message, $headers); 

// Function to encode an image 
function base64_encode_image($filename=string,$filetype=string) { 
    if ($filename) { 
     $imgbinary = fread(fopen($filename, "r"), filesize($filename)); 
     return 'data:image/' . $filetype . ';base64,' . chunk_split(base64_encode($imgbinary), 64, "\n"); 
    } 
} 

このコードに間違っている人はいますか?なぜ私は電子メールを受け取ったときにRAWテキストが表示されているのかわかりません。

+1

を使用しています。 [ではSwiftMailer](http://swiftmailer.org)または[phpmailerの](http://phpmailer.worxware.com)を使用し、この口論のすべてを自分自身を保存します。それは素晴らしいアイデアですが、私はこのプロジェクトの贅沢を持っていない - 彼らは両方の添付ファイルや簡単にインライン画像、 –

+0

@Marcを行います。 –

答えて

0

あなたは、それぞれあなたのメッセージヘッダとMIME取付部ヘッダーの\r\n\n\nを混合しています。すべてを\r\nに変更してみてください。

もう一つの可能​​性 - あなたがイメージではなく、表示をインラインで添付しようとしている場合は、あなた自身のMIMEメールを構築しないでくださいContent-disposition: attachment;

$message .= "Content-Disposition: attachment; filename=\"$file_name\"\n\n"; 
+0

これらの問題を修正したが、引き続き問題が発生しています。 –

+0

@Nicあなたの 'base64_encode_image()'関数ではないのですか?代わりに生のイメージを返すために、普通の 'base64_encode()'を使ってみてください。 –

+0

私は実際にそれがそうであると思ったので、私はあなたが提案したことをやったが、それはまだ動作しません。 –

関連する問題