简体   繁体   English

使用PHP返回以base64编码的电子邮件中的图像

[英]using PHP to return an image in a base64 encoded email

I'm working on an email send and tracking application, but I'm running into a problem that I just can't figure out. 我正在开发电子邮件发送和跟踪应用程序,但是遇到了一个我无法解决的问题。 I want to us a tracking image in my emails but I'm getting some strange behavior. 我想在电子邮件中给我们一个跟踪图像,但是出现了一些奇怪的现象。 Currently, I'm sending out emails encoded in base64. 目前,我正在发送以base64编码的电子邮件。 In the body of the HTML message I'm using an HTML image tag with a link to a PHP file on my server. 在HTML消息的主体中,我正在使用HTML图像标签,并带有指向服务器上PHP文件的链接。 That file sends back an image using the following PHP code: 该文件使用以下PHP代码发送回图像:

header("Content-Type: image/png");
readfile("full-server-path-to-image.png");

This is where it gets strange. 这就是奇怪的地方。 If I view the message in the Outlook email client the image will not display (just shows red "X" image icon). 如果我在Outlook电子邮件客户端中查看该消息,则该图像将不会显示(仅显示红色的“ X”图像图标)。 Other normally linked images will display just fine though. 不过,其他通常链接的图像也可以正常显示。 If I view the same message in Gmail I am able to see all of the images. 如果我在Gmail中查看同一封邮件,则可以看到所有图像。 It's really weird that the image serves up just fine in some email clients but not all of them. 确实奇怪的是,该图像在某些电子邮件客户端(但不是所有客户端)中都能正常显示。

I noticed this problem started when I changed to encoding my messages in base64. 当我更改为在base64中编码消息时,我注意到此问题开始。 Before I started encoding my messages I was able to view all images in any email client. 在开始对消息进行编码之前,我可以在任何电子邮件客户端中查看所有图像。 My gut is telling me this has something to do with the content-encoding-type, but I have no idea how to fix it. 我的直觉告诉我,这与content-encoding-type有关,但我不知道如何解决。 Any help would be appreciated! 任何帮助,将不胜感激!

Here is the PHP code I'm using to send out messages: 这是我用来发送消息的PHP代码:

$myHTMLmessage = chunk_split(base64_encode('
<html><head></head><body>
<img src="https://www.my-web-site.com/openTrack.php?t={trackerid}"></a>
</body></html>'));

$boundary = uniqid('np');               
$to = "somebody-else@another-web-site.com"
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: somebody@my-web-site.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain;charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
//Plain text body
$message .= "some plain text";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html;charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
//Html body
$message .= $myHTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";

mail($to,$subject,$message,$headers);

Just in case anyone runs into something like this I figured out it all boils down to how email clients encode urls. 以防万一有人遇到这样的问题,我发现这全都归结为电子邮件客户端如何编码URL。 In my original code I am using a url with 2 parameters inside my image src attribute. 在我的原始代码中,我在image src属性中使用的URL带有2个参数。 Like this: 像这样:

http://www.my-web-site.com/page.php?pram1=something&pram2=something

When Outlook processes this url it get changed to: 当Outlook处理此网址时,它将更改为:

http://www.my-web-site.com/page.php?pram1=something&amp;pram2=something

Other email clients are smart enough to leave the & symbol the way it is without converting it to &amp; 其他电子邮件客户端足够聪明,可以按原样保留&符号,而无需将其转换为&amp;

In my live code I needed to $_GET both url parameters and process them. 在我的实时代码中,我需要$_GET两个url参数并对其进行处理。 If my script couldn't process both parameters then it wouldn't even get to the line where it would send back an image. 如果我的脚本无法同时处理这两个参数,那么它甚至都不会到达发送图像的行。 The best workaround I could come up with was adding this code in my page.php file: 我能想到的最好的解决方法是在我的page.php文件中添加以下代码:

$pram1 = $_GET['pram1'];
$pram2 = $_GET['pram2'];

if(!isset($pram2)){
    $pram2 = $_GET['amp;pram2'];
}

// now regardless of the email client both parameters contain a value to process

if((isset($pram1)) && (isset($pram2))){
    header("Content-Type: image/png");
    readfile("full-server-path-to-image.png");
}

I know is sounds pretty simple, but I had been scratching my head over this for way too long. 我知道这听起来很简单,但是我为此一直挠头太久了。 Originally, I was convinced it was something to do with how the message was encoded before it was sent out. 最初,我坚信这与消息发送之前的编码方式有关。 Never thought it would be Outlook changing my code. 从来没有想过这将是Outlook更改我的代码。 On second though Microsoft never likes to make things easy.....never gets old...lol 第二,尽管微软从不喜欢让事情变得简单.....永远不会变老...大声笑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM