【问题标题】:how to send an HTML email with an inline attached image with PHP如何使用 PHP 发送带有内联附件图像的 HTML 电子邮件
【发布时间】:2011-11-09 10:58:59
【问题描述】:

我有一个 PHP 脚本,它发送带有附加图像的 HTML 电子邮件。它工作得很好,但是,我无法让附件显示在电子邮件正文的<img> 标记中。附加文件名为postcard.png,服务器上的原始文件名为4e60348f83f2f.png。我尝试将图像 URL 提供为各种内容:cid:postcard.pngcid:4e60348f83f2f.pngpostcard.png4e60348f83f2f.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

这是完整代码:(它基于 php 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>";
?>

【问题讨论】:

  • 使用PHPMailerSwiftmailer。两者都允许内嵌附件,绝对没有任何痛苦,这与您从头开始构建 MIME 消息所经历的不同。
  • 并不是说不能手工完成,而是使用 Swiftmailer 或 PHPMailer 会变得不那么麻烦。 -- 可能与 Send email with PHPMailer - embed image in body 重复
  • @Marc B:我不知道,我用 PHPMailer 敲过一次挂钉,有点痛苦。
  • 我真的不想仅仅为这个单一的脚本包含一个像 PHPMailer 这样的大东西。如果我需要更多的电子邮件功能,我会使用 PHPMailer,但我只需要在这里发送一封带有附件的电子邮件,我已经拥有的脚本非常接近。
  • 如果您自己滚动,除了阅读 RFC 文档之外,您可以尝试使用您最喜欢的电子邮件程序创建一个简单的示例,然后在文本编辑器中打开原始电子邮件以查看它是如何处理的。 (也许是“内容处置:内联”)

标签: php email inline attachment html-email


【解决方案1】:

我终于找到了答案,结果非常简单。 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 或任何其他库更容易,如果这就是你正在做的。毫无疑问,对于更复杂的任务,您会想要获得其中一个库。

【讨论】:

【解决方案2】:

我会推荐使用 PHPMailer 但既然你说你不想我建议这个快速修复。

将绝对 URL 放在 img 标签内,如果您发送 HTML 电子邮件,它会起作用。

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

【讨论】:

  • 谢谢,但是我在发送电子邮件后立即从服务器中删除图像,所以这不起作用,以及因为许多电子邮件客户端不允许在电子邮件中进行热链接。
  • 使用此方法,图像将在各种电子邮件客户端中被阻止
猜你喜欢
  • 2011-11-22
  • 2017-02-23
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多