如何使用PHP发送带有内联附加图像的HTML电子邮件 [英] how to send an HTML email with an inline attached image with PHP

查看:193
本文介绍了如何使用PHP发送带有内联附加图像的HTML电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,它发送一个附有图像的HTML电子邮件。但是,我无法将附件显示在电子邮件正文中的< img> 标签中。所附文件称为 postcard.png ,服务器上的原始文件名为 4e60348f83f2f.png 。我已经尝试将图片网址提供给各种各样的东西: cid:postcard.png cid:4e60348f83f2f.png postcard.png 4e60348f83f2f.png 。没有什么工作。



我认为我在做错事的关键部分在这里,因为这样做使得它成为一个分离的附件,而不是可以使用的内联附件:

 内容传输编码:base64 
Content-Disposition:attachment;
filename =$ fname// ie:postcard.png

我试图改变它来使用一个CID,但是我真的不知道该怎么做,而且这根本就不起作用。

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

这是完整的代码:(它基于这段代码从php中的评论 mail()页面。)

 <?php 
$ to =recipient@email.com ;
$ email =sender@email.com;
$ name =Namename;
$ subject =一个内联图像!;
$ comment =Llookout< b> Llary< / b>它的< br>< b> L1< / 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; //服务器上文件名为
$ 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; //该文件将被命名为


$ attachments ='';
$ Headers =<<<< AKAM
From:$ FromName< $ FromEmail>
回复:$ FromEmail
MIME版本:1.0
内容类型:multipart / mixed;
boundary =$ boundary1
AKAM;

$附件。=<< ATTA
- $ boundary1
内容类型:$ ftype;
name =$ fname
Content-Transfer-Encoding:base64
Content-Disposition:attachment;
filename =$ fname


$附件

ATTA;


$ Body =<< AKAM
这是一个多部分的MIME格式的消息。

- $ boundary1
内容类型:multipart / alternative;
boundary =$ boundary2

- $ boundary2
内容类型:text / plain;
charset =windows-1256
内容转移编码:quoted-printable

$ TextMessage
- $ boundary2
内容类型:为text / html;
charset =windows-1256
Content-Transfer-Encoding:quoted-printable

$ HTMLMessage

- $ boundary2--

$附件
- $ boundary1--
AKAM;

//发送电子邮件
$ ok = mail($ To,$ Subject,$ Body,$ Headers);
echo $ ok?< h1>发送邮件!< / h1>:< h1>邮件未发送!< / h1>;
?>


解决方案

我终于找到了答案,非常简单这个页面是什么帮助我弄清楚,但是我将演示下面要完成的部分。



首先,创建边界字符串,图像正确编码和分块:

  //创建一个边界字符串。它需要是唯一的(不在文本中)所以... 
//我们将使用sha1算法生成一个40个字符的字符串:
$ sep = sha1(date('r' , 时间()));

//现在准备我们的内联图像 - 还读,编码,分割:
$ inline = chunk_split(base64_encode(file_get_contents('figure.gif')));

在电子邮件的HTML部分,图像被引用为这样(使用边界字符串):

 < img src =cid:PHP-CID  -  {$ sep}> 

然后,您可以在内部附件的HTML部分下方创建电子邮件的另一部分,如下所示: / p>

   -  PHP相关 -  {$ sep} 
内容类型:image / gif
Content-转移编码:base64
Content-ID:< PHP-CID - {$ sep}>
{$ inline}

...那就是!比实现PHPmail或其他任何库更容易,如果这是你正在做的。毫无疑问,对于更复杂的任务,您将需要获得其中一个库。


I have a PHP script which sends an HTML email with an attached image. It works beauifully, however, I can't get the attachment to display in an <img> tag in the email body. The attached file is called postcard.png and the original filename on the server is 4e60348f83f2f.png. I've tried giving the image URL as various things: cid:postcard.png, cid:4e60348f83f2f.png, postcard.png, and 4e60348f83f2f.png. Nothing works.

I think the key part that I'm doing wrong is here, because this makes it a separated attachment instead of an inline attachment that I can use:

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

I've tried changing it to use a CID but I don't really know how to do that, and this didnt' work at all:

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

Here's the full code: (It's based on this code from a comment in the php mail() page.)

<?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>";
?>

解决方案

I finally found the answer, which turns out to be remarkably simple. This page is what helped me figure it out, but I'll demonstrate the parts that I needed to get it done below.

First off, there's the creation of the boundary string, and the image, correctly encoded and chunked:

// 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')));

In the HTML part of the email, the image is referenced like this (using the boundary string):

<img src="cid:PHP-CID-{$sep}">

Then you create another part of the email below the HTML part for the inline attachment, like this:

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

...and that is that! Easier than implementing PHPmailer or any of the other libraries, if this is all you're doing. No doubt for more complicated task, you'll want to get one of those libraries.

这篇关于如何使用PHP发送带有内联附加图像的HTML电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆