첨부 된 이미지가있는 HTML 전자 메일을 보내는 PHP 스크립트가 있습니다. 하지만 필자는 이메일 본문의 <img>
태그에 첨부 파일을 표시 할 수 없습니다. 첨부 된 파일의 이름은 이고 서버의 원래 파일 이름은 4e60348f83f2f.png
입니다. 여러 가지로 이미지 URL을 제공하려고 시도했습니다. cid:postcard.png
, cid:4e60348f83f2f.png
, postcard.png
및 4e60348f83f2f.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>";
?>
[PHPMailer] (http://phpmailer.worxware.com) 또는 [Swiftmailer] (http://swiftmailer.org)를 사용하십시오. 둘 다 MIME 메시지를 처음부터 만들려고하는 것과는 달리 인내심이 전혀없는 인라인 첨부를 허용합니다. –
손으로 직접 할 수있는 것은 아니지만 Swiftmailer 또는 PHPMailer를 사용하는 것이 덜 부담 스럽습니다. - [PHPMailer와 함께 이메일 보내기 - 몸에 이미지 삽입] (http://stackoverflow.com/questions/3708153/send-email-with-phpmailer-embed-image-in-body) – mario
@Marc B : 모르겠다. PHPMailer를 사용하여 한 번 입력 한 행 아웃을 꺼냈다. 다소 고통 스러웠다. –