2011-09-02 3 views
5

첨부 된 이미지가있는 HTML 전자 메일을 보내는 PHP 스크립트가 있습니다. 하지만 필자는 이메일 본문의 <img> 태그에 첨부 파일을 표시 할 수 없습니다. 첨부 된 파일의 이름은 이고 서버의 원래 파일 이름은 4e60348f83f2f.png입니다. 여러 가지로 이미지 URL을 제공하려고 시도했습니다. cid:postcard.png, cid:4e60348f83f2f.png, postcard.png4e60348f83f2f.png 아무것도 작동하지 않습니다.PHP로 인라인 첨부 이미지가있는 HTML 전자 메일을 보내는 방법

Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
    filename="$fname" // i.e.: "postcard.png" 

내가 해봤 사용하도록 변경 :

나는이가 내가 사용할 수있는 대신 인라인 첨부 파일의 분리 된 첨부 파일 만들기 때문에 내가 잘못 키 부분은 여기에 생각 CID와는하지만 난 정말 어떻게 해야할지하지 않으며, 모든이 didnt 한 '작품 : 여기에

Content-Transfer-Encoding: base64 
Content-ID: <$fname> // i.e.: postcard.png 

는 전체 코드입니다 : (그것은 PHP는 mail() 페이지에서 댓글에서 this code 기반으로.)

<?php 
$to  = "[email protected]"; 
$email = "[email protected]"; 
$name = "Namename"; 
$subject = "An inline image!"; 
$comment = "Llookout <b>Llary</b> it's <br> the <b>Ll</b>andllord!<br><img src='cid:postcard.png'><br><img src='cid:4e60348f83f2f.png'><img src='postcard.png'><br><img src='4e60348f83f2f.png'>"; 

$To   = strip_tags($to); 
$TextMessage =strip_tags(nl2br($comment),"<br>"); 
$HTMLMessage =nl2br($comment); 
$FromName =strip_tags($name); 
$FromEmail =strip_tags($email); 
$Subject  =strip_tags($subject); 

$boundary1 =rand(0,9)."-" 
    .rand(10000000000,9999999999)."-" 
    .rand(10000000000,9999999999)."=:" 
    .rand(10000,99999); 
$boundary2 =rand(0,9)."-".rand(10000000000,9999999999)."-" 
    .rand(10000000000,9999999999)."=:" 
    .rand(10000,99999); 

$filename1 = "4e60348f83f2f.png"; //name of file on server with script 
$handle  =fopen($filename1, 'rb'); 
$f_contents =fread($handle, filesize($filename1)); 
$attachment=chunk_split(base64_encode($f_contents)); 
fclose($handle); 

$ftype  ="image/png"; 
$fname  ="postcard.png"; //what the file will be named 


$attachments=''; 
$Headers  =<<<AKAM 
From: $FromName <$FromEmail> 
Reply-To: $FromEmail 
MIME-Version: 1.0 
Content-Type: multipart/mixed; 
    boundary="$boundary1" 
AKAM; 

$attachments.=<<<ATTA 
--$boundary1 
Content-Type: $ftype; 
    name="$fname" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
    filename="$fname" 


$attachment 

ATTA; 


$Body  =<<<AKAM 
This is a multi-part message in MIME format. 

--$boundary1 
Content-Type: multipart/alternative; 
    boundary="$boundary2" 

--$boundary2 
Content-Type: text/plain; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$TextMessage 
--$boundary2 
Content-Type: text/html; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$HTMLMessage 

--$boundary2-- 

$attachments 
--$boundary1-- 
AKAM; 

// Send email 
$ok=mail($To, $Subject, $Body, $Headers); 
echo $ok?"<h1> Mail sent!</h1>":"<h1> Mail not sent!</h1>"; 
?> 
+2

[PHPMailer] (http://phpmailer.worxware.com) 또는 [Swiftmailer] (http://swiftmailer.org)를 사용하십시오. 둘 다 MIME 메시지를 처음부터 만들려고하는 것과는 달리 인내심이 전혀없는 인라인 첨부를 허용합니다. –

+0

손으로 직접 할 수있는 것은 아니지만 Swiftmailer 또는 PHPMailer를 사용하는 것이 덜 부담 스럽습니다. - [PHPMailer와 함께 이메일 보내기 - 몸에 이미지 삽입] (http://stackoverflow.com/questions/3708153/send-email-with-phpmailer-embed-image-in-body) – mario

+1

@Marc B : 모르겠다. PHPMailer를 사용하여 한 번 입력 한 행 아웃을 꺼냈다. 다소 고통 스러웠다. –

답변

14

마침내 대답이 눈에 띄게 단순한 것으로 나타났습니다. This page은 그 점을 이해하는 데 도움이되었지만 아래에 설명하는 데 필요한 부분을 보여줄 것입니다.

첫째는, 경계 문자열의 생성이, 그리고 이미지가 제대로 인코딩 청크 : 이메일의 HTML 부분에서

// Create a boundary string. It needs to be unique (not in the text) so ... 
// We are going to use the sha1 algorithm to generate a 40 character string: 
$sep = sha1(date('r', time())); 

// Also now prepare our inline image - Also read, encode, split: 
$inline = chunk_split(base64_encode(file_get_contents('figure.gif'))); 

이 이미지는 다음과 같이 참조 (경계 문자열을 사용하여) :
<img src="cid:PHP-CID-{$sep}"> 

그런 다음 당신은 다음과 같이 인라인 첨부 파일에 대한 HTML 부분 아래에 이메일의 다른 부분을 만들 :

--PHP-related-{$sep} 
Content-Type: image/gif 
Content-Transfer-Encoding: base64 
Content-ID: <PHP-CID-{$sep}> 
{$inline} 
,

... 그게 바로! PHPmailer 나 다른 라이브러리를 구현하는 것보다 쉽습니다. 더 복잡한 작업에 대해서는 의심의 여지가 없습니다. 이러한 라이브러리 중 하나를 얻고 싶습니다.

+0

이 답변에 따라 첨부 할 수 없습니다. http://pastebin.com/z8LqAHCB –

0

PHPMailer를 사용하는 것이 좋습니다. 그러나이 빠른 수정 방법을 제안하지 않으려한다고 말한 이후로 말입니다.

img 태그 안에 절대 URL을 넣으면 HTML 이메일을 보내면 작동합니다.

<img src="http://example.com/image.jpg"/> 
+1

감사합니다. 그러나 이메일을 보낸 직후 서버에서 이미지를 삭제하므로 많은 이메일 클라이언트가 이메일에서 핫 링크를 허용하지 않기 때문에 작동하지 않습니다. – brentonstrine

+0

이 방법을 사용하면 이미지가 다양한 이메일 클라이언트에서 차단됩니다. – ljelewis

관련 문제