PHP:检查谁阅读了发送的电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4603850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Check who had read sent email?
提问by PHP Ferrari
I am sending email to some users and wants to know who had read it, means if some one had read that email then a log file will maintain which contain the email address of that user with date/time/IP. For this I send a javascript function with the email (html template) which just alert the email address of the user when ever a user opens that email like:
我正在向一些用户发送电子邮件并想知道谁阅读了它,这意味着如果有人阅读了该电子邮件,那么将维护一个日志文件,其中包含该用户的电子邮件地址和日期/时间/IP。为此,我发送了一个带有电子邮件(html 模板)的 javascript 函数,它只会在用户打开该电子邮件时提醒用户的电子邮件地址,例如:
for($n=0; $n<sizeof($checkBox); $n++){
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->Subject = $subject;
$function = "<script language='javascript'>function stats(emailId){alert(emailId);}</script>";
$bodyOpen = "<body onload='stats(".$checkBox[$n].");'>";
$msg_body .= $body .= "<table><tr><td>Hello Everyone</td></tr></table></body>";
$mail->Body = $function.$bodyOpen.$msg_body;
$mail->WordWrap = 50;
$mail->FromName = 'Muhammad Sajid';
$mail->IsMAIL();
$mail->From = '[email protected]';
$mail->AddAddress($checkBox[$n]);
$sent = $mail->Send();
}
the html template works fine and shows an alert popup on page load but it does not works if I use to send this html template.
html 模板工作正常,并在页面加载时显示警报弹出窗口,但如果我使用发送此 html 模板,则它不起作用。
And I only want to solve this issue using PHP5.x.x / javascript, no other software or third party tool. Any help..?
而且我只想使用PHP5.xx / javascript解决这个问题,没有其他软件或第三方工具。任何帮助..?
回答by Populus
Add Header to email:
将标题添加到电子邮件:
Disposition-Notification-To: [email protected]
As mentioned above it's not reliable and it's better to do something like this:
如上所述,它不可靠,最好执行以下操作:
<img src="http://yourdomain.com/emailreceipt.php?receipt=<email of receiver>" />
And log it in a database, although again this is restricted by the email client's ability to show images and sometimes it may even put the mail into junk because it doesn't detect an image... a workaround that would be to actually outputting an image (say your logo) at the end of that script.
并将其登录到数据库中,尽管这再次受到电子邮件客户端显示图像能力的限制,有时它甚至可能将邮件放入垃圾邮件中,因为它没有检测到图像......一种解决方法是实际输出一个该脚本末尾的图像(比如您的徽标)。
Edit: A quick lookup at the phpmailer class gave me the following:
编辑:快速查找 phpmailer 类给了我以下信息:
$mail->ConfirmReadingTo = '[email protected]';
but it's the same as the Disposition-Notification-To method above.
但它与上面的 Disposition-Notification-To 方法相同。
回答by trex005
Send a beacon image in the emails like so
像这样在电子邮件中发送信标图像
<img src='http://www.yourserver.com/beacon.php?email_id=$email_id&email_address=$user_address' style='width:1px;height:1px'>
And then use the beacon.php file to log the data. You will then want to output a 1X1 image with appropriate headers.
然后使用beacon.php 文件记录数据。然后,您将希望输出带有适当标题的 1X1 图像。
Important note
Many popular email clients (such as Gmail) now block external images, so this is by far, not fool proof.
重要说明
许多流行的电子邮件客户端(例如 Gmail)现在会阻止外部图像,因此到目前为止,这还不是万无一失的。
回答by Jake N
This is next to impossible to do 100% effectively.
这几乎不可能 100% 有效地完成。
You could control where the content is stored e.g. http://www.example.com/34hg038g85gb8no84g5and provide a link in the email to that content, you can then detect when that URL was viewed.
Use a method used by MailChimp and other newsletter campaigns, put an invisible image in your email, this image should reside on a server you control, you can then detect when that image is hit when the user opens the email.
您可以控制内容的存储位置,例如http://www.example.com/34hg038g85gb8no84g5并在电子邮件中提供指向该内容的链接,然后您可以检测该 URL 何时被查看。
使用 MailChimp 和其他时事通讯活动使用的方法,在您的电子邮件中放置一个不可见的图像,该图像应位于您控制的服务器上,然后您可以在用户打开电子邮件时检测该图像何时被点击。
回答by zerkms
It is not possible by definition.
根据定义,这是不可能的。
回答by Sondre
Mailreaders are not browsers, they don't support javascript. They don't even support proper CSS so dont expect too much. So I honestly don't see any way you can do what you're trying to do
邮件阅读器不是浏览器,它们不支持 javascript。他们甚至不支持正确的 CSS,所以不要期望太多。所以老实说,我看不出有什么办法可以做你想做的事
回答by PHP Ferrari
I just add a single line:
我只添加一行:
$dt = date('F \ jS\,\ Y h:i:s a');
for($n=0; $n<sizeof($checkBox); $n++){
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->Subject = $subject;
$src = "<img src='msajid.isgreat.org/readmail.php?dt=".$dt."&eid=".$checkBox[$n]."' />";
$msg_body .= $src .= "<table><tr><td>Hello Everyone</td></tr></table>";
$mail->Body = $function.$bodyOpen.$msg_body;
$mail->WordWrap = 50;
$mail->FromName = 'Muhammad Sajid';
$mail->IsMAIL();
$mail->From = '[email protected]';
$mail->AddAddress($checkBox[$n]);
$sent = $mail->Send();
}
and in readmail.phpfile simply insert date/time and userid with a check (if not exist with attached date/time) & fixed it only for Gmail, hotmail but not for Yahoo... Can some one help to also fix for Yahoo....?
并在readmail.php文件中简单地插入日期/时间和带有检查的用户 ID(如果不存在附加日期/时间)并仅针对 Gmail、hotmail 而不是针对 Yahoo 修复它......有人可以帮助也修复 Yahoo ……?
Haaaa. silly mistake just use complete url like:
哈哈。愚蠢的错误只是使用完整的网址,如:
$src = "<img src='http://www.msajid.isgreat.org/readmail.php?dt=".$dt."&eid=".$checkBox[$n]."' />";
and it will also work for Yahoo....
它也适用于雅虎......
回答by Bryce Morrison
While I didn't discover exactly why the simple PHP file wasn't generating the included image (as mentioned in my post 6 hours ago), here is another very complicated way of generating an image file that wasn'trejected by my own PHP 5.4.30 web server.
虽然我没有确切地发现为什么简单的 PHP 文件没有生成包含的图像(如我 6 小时前的帖子所述),但这里有另一种非常复杂的生成图像文件的方法,该方法没有被我自己的 PHP 拒绝5.4.30 网络服务器。
Here is the code that I put into an index.php file within an /email_image/ subdirectory:
这是我放入 /email_image/ 子目录中的 index.php 文件的代码:
<?php
$message_id = $_REQUEST['message_id'];
$graphic_http = 'http://mywebsite.com/email_image/message_open_tracking.gif';
$filesize = filesize( 'message_open_tracking.gif' );
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private',false );
header( 'Content-Disposition: attachment; filename="a_unique_image_name_' . $message_id . '.gif"' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: '.$filesize );
readfile( $graphic_http );
exit;
?>
For the image filename, I used the following:
对于图像文件名,我使用了以下内容:
http://mywebsite.com/email_image/?message_id=12345
Within the email_image folder is also a blank 1x1 gif image named "message_open_tracking.gif".
email_image 文件夹中还有一个空白的 1x1 gif 图像,名为“message_open_tracking.gif”。
The index.php file can also be revised to make use of the message_id in order to mark that message as having been read. If other variables are included within the querystring, such as the recipient's email address, those values can also be used within that index.php file.
还可以修改 index.php 文件以使用 message_id 来标记该消息已被阅读。如果查询字符串中包含其他变量,例如收件人的电子邮件地址,则这些值也可以在该 index.php 文件中使用。
Many thanks to Bennett Stone for the following article: http://www.phpdevtips.com/2013/06/email-open-tracking-with-php-and-mysql/
非常感谢 Bennett Stone 的以下文章:http: //www.phpdevtips.com/2013/06/email-open-tracking-with-php-and-mysql/
回答by Ron Robinson
I know this is an old thread but I just had to respond... for those of us who must consider this, I suggest we put ourselves in the place of the people who have to write the anti-spam software to thwart our efforts. Detecting a ? or a .php (or other script/binary extension) inside an image tag would be trivial for me as a postulated 'spam assassin' programmer... just as identifying a 1x1 image would be...
我知道这是一个旧线程,但我只是不得不回应......对于我们这些必须考虑这一点的人,我建议我们将自己置于必须编写反垃圾邮件软件以阻挠我们努力的人的位置。检测 ? 或者图像标签内的 .php(或其他脚本/二进制扩展名)对我来说是微不足道的,因为我是一个假定的“垃圾邮件杀手”程序员……就像识别 1x1 图像一样……
I spent 2.5 yrs as National Digital Director for a presidential campaign that raised $20 million before we even had a candidate-- here's what I had developed for that campaign:
我在总统竞选活动中担任了 2.5 年的国家数字总监,在我们甚至没有候选人之前就筹集了 2000 万美元——这是我为该竞选活动制定的:
At send time (or before), generate a hash on the TO: email address and store that in the db next to the email address.
Use the hash we just generated to modify a small, but plainly visible logo in the email and make a copy of the logo with the hash in the logo file name eg: emxlogox.0FAE7E6.png - refer to that unique image in the email for the logo - make a copy of the logo with the hash name in the filename. This particular logo series only ever appears in targeted mass emails. Warn crew members not to copy it for other purposes (or to rename it extensively if they do). The first part of the filename needs to be something that will not appear in the logs in other contexts to speed your parsing and the code you have to craft to sort out false hits.
Parse the logs for occurrences of the logo being requested, and extract the hash from the filename to match back against the one email address. And your parsing program can also get the IP address and the time delta it took for them to get and open the email so you can identify highly responsive recipients and the ones who took a week to open the email. Do a geo-lookup on the IP and see if you get a match with the location you already have, and/or start recording their travel patterns (or proxy use patterns). Geo deltas could also be identifying email forwards.
在发送时(或之前),在 TO: 电子邮件地址上生成一个哈希值并将其存储在电子邮件地址旁边的数据库中。
使用我们刚刚生成的哈希值来修改电子邮件中一个小但清晰可见的徽标,并使用徽标文件名中的哈希值制作徽标的副本,例如:emxlogox.0FAE7E6.png - 请参阅电子邮件中的唯一图像徽标 - 在文件名中使用哈希名称制作徽标的副本。这个特定的标志系列只出现在有针对性的群发电子邮件中。警告机组人员不要将其复制用于其他目的(或者如果他们这样做,则对其进行广泛的重命名)。文件名的第一部分必须是不会出现在其他上下文中的日志中的内容,以加快您的解析和您必须制作的代码以整理出错误命中。
解析日志中所请求徽标的出现情况,并从文件名中提取哈希值以匹配一个电子邮件地址。并且您的解析程序还可以获得 IP 地址以及他们获取和打开电子邮件所需的时间增量,因此您可以识别响应迅速的收件人以及需要一周时间打开电子邮件的收件人。对 IP 进行地理查找,看看您是否与您已有的位置匹配,和/或开始记录他们的旅行模式(或代理使用模式)。地理增量也可以识别电子邮件转发。
Same hash, of course, is used to record clicks, and also the first and second opt-ins. (Now you have a 'dossier' of multiple opt-ins for responses to those abuse reports and you're protecting your email reputation, too).
当然,相同的哈希用于记录点击次数,以及第一次和第二次选择加入。(现在您有一个“档案”,其中包含多个选择加入对这些滥用报告的回复,并且您也在保护您的电子邮件声誉)。
This method can also be used to identify who forwards emails to their friends and you ask those 'good forwarders' to join some kind of elite digital volunteer crew, offer them discounts or rewards or whatever is appropriate for your business/project... in essence, that same hash also becomes a referrer code.
这种方法还可用于识别谁将电子邮件转发给他们的朋友,并且您要求那些“优秀的转发者”加入某种精英数字志愿者团队,为他们提供折扣或奖励或任何适合您的业务/项目的...本质上,相同的散列也成为引用代码。
You can also ask them to right click on the logo in the email and save it without changing the filename, then post it to 'wherever' (the image should have a memorable, readable, meaningful shortlink on it, not an unreadable bit.ly shortlink). You can then use the Google Search API to identify who helped you out in that fashion and thank them or give them rewards... For this purpose, it helps if the first part of your logo filename is really unique like unsportingly.unique.emxlogox.0FAE7E6.png so you don't have to do millions of Google Search API queries - just search on the unique first part of the filename - and look at the hashes after you get the hits.
您还可以要求他们右键单击电子邮件中的徽标并保存它而不更改文件名,然后将其发布到“任何地方”(图像上应该有一个令人难忘、可读、有意义的短链接,而不是一个不可读的位。ly短链接)。然后,您可以使用 Google 搜索 API 来确定谁以这种方式帮助过您,并感谢他们或给予他们奖励......为此,如果您的徽标文件名的第一部分真的像 unsportingly.unique.emxlogox 这样独一无二,这会有所帮助.0FAE7E6.png 这样您就不必进行数百万次 Google Search API 查询 - 只需搜索文件名的唯一第一部分 - 并在获得命中后查看哈希值。
You store the links where their copy of the logo appeared in your db to add to your dossier of where on the net they are active and have helped you.
您存储他们的徽标副本出现在您的数据库中的链接,以添加到您的档案中,说明他们在网络上的活跃位置并为您提供了帮助。
Yes, it's slow and burdensome, but in these days when we say we want to develop a 'relationship' with our email list, it's what you have to do to give individual treatment; identify and reward your friends. And yes, you end up with millions of those hashed filename images in one directory, but storage is cheap and it's worth it to really have that relationship with your peeps.
是的,这是缓慢而繁重的,但在这些日子里,当我们说我们想与我们的电子邮件列表建立“关系”时,这是您必须做的才能给予个人治疗;识别和奖励你的朋友。是的,您最终会在一个目录中拥有数百万个经过哈希处理的文件名图像,但是存储空间很便宜,而且与您的窥视者真正建立这种关系是值得的。
回答by Maxi
Embedding a user specific image (1px blank might be good) in the email and record whether it is hot or not is a fair solution. But the problem Gmail like client block external images by default.
在电子邮件中嵌入用户特定的图像(1px 空白可能很好)并记录它是否热是一个公平的解决方案。但问题 Gmail 像客户端默认阻止外部图像。