我有一个PHP脚本,它发送带有附加图像的HTML电子邮件。它工作得很漂亮,但是,我无法在电子邮件正文中的<img>
标记中显示附件。附加文件名为postcard.png
,服务器上的原始文件名为4e60348f83f2f.png
。我尝试将图片网址作为各种内容提供:cid:postcard.png
,cid:4e60348f83f2f.png
,postcard.png
和4e60348f83f2f.png
。什么都行不通。
我认为我做错的关键部分在这里,因为这使它成为一个单独的附件,而不是我可以使用的内联附件:
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$fname" // i.e.: "postcard.png"
我已经尝试将其更改为使用CID,但我真的不知道该怎么做,而且这根本不起作用:
Content-Transfer-Encoding: base64
Content-ID: <$fname> // i.e.: postcard.png
以下是完整代码:(它基于mail()
页面评论中的this code。)
<?php
$to = "recipient@email.com";
$email = "sender@email.com";
$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>";
?>
答案 0 :(得分:14)
我终于找到了答案,结果非常简单。 This page帮助我解决了这个问题,但我将在下面展示我需要完成的部分。
首先,创建了边界字符串,以及正确编码和分块的图像:
// 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')));
在电子邮件的HTML部分中,图像被引用(使用边界字符串):
<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或任何其他库更容易,如果你正在做的那样。毫无疑问,对于更复杂的任务,你需要获得其中一个库。
答案 1 :(得分:0)
我建议使用PHPMailer但是因为你说你不想我建议这个快速修复。
将绝对URL放在img标记内,如果你确实发送了一封HTML电子邮件,它就可以了。
<img src="http://example.com/image.jpg"/>