2011-08-11 4 views
2

이미지를 base64로 인코딩하려고 시도했지만 php mail()을 사용하여 이메일 첨부 파일로 보냈지 만 이메일에 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으로 변경해보십시오.

또 다른 가능성 - 당신은 이미지보다는 인라인 표시를 부착하려고하는 경우, 자신의 마임 이메일을 구축하지 마십시오 Content-disposition: attachment;

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

이러한 문제가 해결되어 여전히 문제가 있습니다. –

+0

@Nic'base64_encode_image()'함수가 아닌 것이 확실합니까? 원시 이미지를 반환하려면 일반'base64_encode()'를 사용하십시오. –

+0

나는 그것이 실제로 그렇게 될지도 모른다고 생각했다. 그래서 나는 당신이 제안한 것을했는데, 여전히 효과가 없다. –

관련 문제