简体   繁体   English

PHP HTML生成的电子邮件缓存图像,从而显示旧图像

[英]PHP HTML generated email caches images, thus displaying old images

I'm using PHP to generate an html email which sends my clients latest stats in graph form. 我正在使用PHP生成HTML电子邮件,该电子邮件将图形形式的最新统计信息发送给我的客户。 PHP generates a new image everytime it sends out stats with the same image name to prevent high disk space usage. PHP每次发送具有相同映像名称的统计信息时,都会生成一个新映像,以防止占用大量磁盘空间。 Now my problem is the image gets cached thus displaying the old image to the client instead of the new image. 现在我的问题是图像被缓存,因此向客户端显示旧图像而不是新图像。

My html headers look as follows. 我的html标头如下所示。

"From: Test <test@test.com>\n"
      // . "To: " . $contact . " <" . $email . ">\n"
       . "To: myemail@test.com\n"
       . "X-Confirm-Reading-To: test@test.com\n"
       . "Disposition-Notification-To: test@test.com\n"
       . "MIME-Version: 1.0\n"
       . "Content-Type: multipart/mixed;"
       . ' boundary="PAA08673.1018277622/www.test.com"'
       . "\nSubject: Stats for $name\n\n"
       . "This is a MIME-encapsulated message\n\n"
       . "--PAA08673.1018277622/test@test.com"
       . "\nContent-Type: text/html\n\n";

How can I force the mail to download the latest generated image from the server? 如何强制邮件从服务器下载最新生成的图像?

Include something extra in the URL, like the timestamp of the graph image 在URL中包括一些额外的内容,例如图形图像的时间戳

<img src="http://example.com/graphs/graph.png?t=1263283697">

That way, the URL changes whenever the image does. 这样,只要图片出现,URL就会更改。 This won't stop the user-agent from caching what it sees, so it still might show an old image even after the server updates. 这不会阻止用户代理缓存所看到的内容,因此即使在服务器更新后,它仍可能显示旧图像。

So, if you want to stop the user-agent from actually caching the image, then write a script which returns the image with some headers to prevent caching.... 因此,如果您要停止用户代理实际缓存图像,请编写脚本以返回带有一些标头的图像以防止缓存。

$file="graph.png";
$size=filesize($file);

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Length: $size");
header("Content-Type: image/png");

readfile($file);

Have the filename itself include a timestamp. 让文件名本身包含一个时间戳。 So instead of overwriting the old image, you delete it first (thus insuring it's really gone) and replace it with the newer image with the newer image name. 因此,无需覆盖旧图像,而是先删除它(以确保它确实消失了),然后用具有新图像名称的新图像替换它。

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

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