php 我怎么知道有人打开了电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20729172/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:28:41  来源:igfitidea点击:

How can i know if someone has opened an email?

phpmass-emailscampaign-monitor

提问by Naveed Metlo

I am working on an email system (PHP based) where hundreds of emails will be sent by the users and i want to track those emails so i will be able to know that email is opened or not ?

我正在开发一个电子邮件系统(基于 PHP),其中用户将发送数百封电子邮件,我想跟踪这些电子邮件,以便我能够知道电子邮件是否已打开?

Can any one guide me how to do this ?

任何人都可以指导我如何做到这一点?

Thanks

谢谢

回答by Kiran RS

The only way I know of - and it's not very reliable - is to send an HTML mail in which you include something like:

我所知道的唯一方法 - 它不是很可靠 - 是发送一个 HTML 邮件,其中包含以下内容:

PHP Code:

PHP代码:

<img src='http://www.domain.com/mailcheck.php?user=123'>

image but in the process, you can track the GET user. You can also find a way to modifier the mail header so as to request a receipt - but I don't know how to do that - and it's also not reliable because of the voluntary nature of the request.

image 但在此过程中,您可以跟踪 GET 用户。您还可以找到一种方法来修改邮件标题以请求收据 - 但我不知道该怎么做 - 而且由于请求的自愿性质,它也不可靠。

回答by sushant-hiray

Simple, you set up a PHP script that returns a 1x1 image. Have that script log the User-Agent and the IP (You can even log the referrer).

很简单,您设置了一个返回 1x1 图像的 PHP 脚本。让该脚本记录用户代理和 IP(您甚至可以记录引用者)。

Now embed that in the email.

现在将其嵌入电子邮件中。

Since gmail has started showing the image always, but will host them from its own servers, you can although get to know if the mail is opened, but you might not be able to track the correct IP. Check some reference here: Effect of gmail caching and showing images by default

由于 gmail 已开始始终显示图像,但将从其自己的服务器托管它们,因此您可以了解邮件是否已打开,但您可能无法跟踪正确的 IP。在此处查看一些参考:默认情况下 gmail 缓存和显示图像的效果

You can get to know how mailchimp does it: MailChimp Working

您可以了解 mailchimp 是如何做到的:MailChimp 工作

EDIT: Code reference:

编辑:代码参考:

<img src="http://www.example.com/checkopen.php?user_id=20" />

Inside the checkopen.phpscript, get the user_idfield, now corresponding to this field, store it that this user has opened the mail.

checkopen.php脚本里面,获取user_id字段,现在对应这个字段,存储这个用户打开了邮件。

While sending the mail, make sure you increment the user_idfield everytime you send the mail.

发送邮件时,请确保user_id每次发送邮件时都增加该字段。

So whenever this image is rendered, it will call the corresponding url and thus you can log into your system about the status of mail being opened.

因此,无论何时渲染此图像,它都会调用相应的 url,因此您可以登录系统了解正在打开的邮件的状态。

回答by Noufal Ibrahim

Email has a disposition notification fieldwhich you can use to achieve this. It, of course, depends on the MUA at the remote end. They're free to ignore the MDN (which is a polite request rather than something subversive which the other 2 answers suggest). The MUA of your final recipient will send an email back to you saying that your message has been read. Your SMTP server can filter these and you can run some kind of program that maps sent emails to responses.

电子邮件有一个处置通知字段,您可以使用它来实现此目的。当然,这取决于远端的 MUA。他们可以随意忽略 MDN(这是一个礼貌的请求,而不是其他 2 个答案所暗示的具有颠覆性的东西)。您最终收件人的 MUA 将向您发送一封电子邮件,告知您的消息已被阅读。您的 SMTP 服务器可以过滤这些,您可以运行某种程序将发送的电子邮件映射到响应。

回答by Bryce Morrison

While I didn't discover exactly why the simple PHP file wasn't generating the included image, here is another very complicated way of generating an image file that wasn't rejected by my own PHP 5.4.30 web server.

虽然我没有发现为什么简单的 PHP 文件没有生成包含的图像,但这里有另一种非常复杂的生成图像文件的方法,它没有被我自己的 PHP 5.4.30 Web 服务器拒绝。

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 Hemant Kumar

Here is link to complete guide

这是完整指南的链接

[ [ Prerequisite: ] ] 1. You need download PHPMailerAutoload.php. 2. Please check your php version. 3. Please check your shared hosting allows phpmailer.

[ [前提:] ] 1.您需要下载PHPMailerAutoload.php。2. 请检查您的 php 版本。3. 请检查您的共享主机是否允许 phpmailer。

[ [ Privacy Policy ] ] 1. A recipient mail service provider obviously pops up a privacy alert when the recipient opens your email. So if he or she rejects your email, the desirable information could not be gained.

[ [隐私政策] ] 1.收件人邮件服务提供商在收件人打开您的电子邮件时显然会弹出隐私警报。因此,如果他或她拒绝了您的电子邮件,则无法获得所需的信息。

Step 1. Create a PHP web page with name “index.php”. This will serve as our front-end.We'll send an email and track it from here only.

步骤 1. 创建一个名为“index.php”的 PHP 网页。这将作为我们的前端。我们将发送电子邮件并仅从这里进行跟踪。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Email Open Tracking Using PHP</title>
        <script src="js/jquery.min.js" type="text/javascript"></script>              
     <script src="js/myjs.js" type="text/javascript"></script>
        <link href="css/style.css" rel="stylesheet" type="text/css"/>

    </head> 
    <body>        
        <div id="main">
            <h1>Email Open Tracking Using PHP</h1>
            <div id="login">
                <h2>Send Email</h2>
                <hr/>
                <form id="form1"  method="post">
                    <div id="box">
                        <input type="email" placeholder="To : Email Id " name="mailto" required/>  
                        <input type="text" placeholder="Subject : " name="subject" required/>
                        <textarea rows="2" cols="50" placeholder="Meassage : This is the fixed message of test email to get notify when it is read...." name="message" readonly ></textarea>
                        <input type="submit" value="Send" name="send" />   
                    </div>                                     
                </form>

                <div id="loading-image"><img src="http://www.arabianbusiness.com/skins/ab.main/gfx/loading_spinner.gif" alt="Sending....."/></div>

                <form id="form2"  method="post">   
                        <div id="view"></div><br><br>
                    <div id="readstatus"></div>
                    <input type="submit" value="Track Status" id="track_mail" name="track"/>                           
                </form>
            </div>
        </div>
    </body>
</html>

Step 2. Create a PHP file with name “tracker.php”. There will be our PHP script for two purpose:

步骤 2. 创建一个名为“tracker.php”的 PHP 文件。我们的 PHP 脚本有两个用途:

a. Send mail using PHP mailer library.

一种。使用 PHP 邮件程序库发送邮件。

b. Read the log file(email.txt) to track if the email opened.

湾 阅读日志文件 (email.txt) 以跟踪电子邮件是否打开。

<?php

require ('phpmailer/PHPMailerAutoload.php');
$from = "[email protected]";    //sender's username
$pwd = "test@1234";         //sender's password
//-------------------------------------------------------SEND eMail----------------------------------------------------------------------
if (isset($_POST['mailto'])) {
    try {
        $mail = new PHPMailer(true); //New instance,exceptions enabled with true
        $to = $_POST['mailto'];
        $subject = $_POST['subject'];
        $id = rand(111, 999);
        $id.=rand(111, 999);
        $body = "This is the fixed message of test email to get notify when it is read.....";
        $body .= "<img border='0' src='https://yourwebsite.com/trackonline.php?email=$to&id=$id&subject=$subject' width='1' height='1' alt='image for email' >";
        $mail->IsSMTP();                           // tell the class to use SMTP
        $mail->SMTPAuth = true;                  // enable SMTP authentication
        $mail->Port = 25;                    // set the SMTP server port
        $mail->Host = "smtp.gmail.com"; // SMTP server
        $mail->Username = $from;     // SMTP server username
        $mail->Password = $pwd;            // SMTP server password
        $mail->From = $from;
        $mail->FromName = "TESTUSER";
        $mail->AddAddress($to);
        $mail->Subject = $subject;
        $mail->AltBody = "Please return read receipt to me."; // optional, comment out and test
        $mail->WordWrap = 80; // set word wrap
        $mail->MsgHTML($body);
        $mail->IsHTML(true); // send as HTML
        $mail->Send();

//return foll
        echo '<input id="id1" name="id" type="hidden" value="' . $id . '">'   
        . '<input id="email1" name="email" type="hidden" value="' . $to . '">'
        . '<label id="label1">Mail sent to <b>' . $to . '<b></label>';
    } catch (phpmailerException $e) {
        echo $e->errorMessage();
    }
}
////------------------------------------------READ email.txt-------------------------------------------------------

if (!empty($_POST['id'])) {

    $id = $_POST['id'];
    $to = $_POST['email'];
    $checkid = "Id:" . $id;
    $fh = fopen("https://yourwebsite.com/email.txt", "r");
    $read = false; // init as false
    while (($buffer = fgets($fh)) !== false) {
        if (strpos($buffer, $checkid) !== false) {
            $a = explode("%",$buffer);
            $read = true;
            break; // Once you find the string, you should break out the loop.
        }
    }
    fclose($fh);

    if ($read == true) {
        //$string = $email . " seen the mail on subject: '" . $sub . "' from ip: " . $ipAddress . " on " . $date . " and Id:" . $id . "\n";
        echo "<img id=\"closed-image\" src=\"img/envelope-open.png\" alt=\"email not opened\"/><br><p id=\"closed-para\">"
        . "Mail sent from <b>" . $from . "</b><br> To <b>" . $to
        . "</b><br>has been<div id=\"color-read\"> opened on <b>".$a[1]."</b></div></p>"
        . "<input id=\"id1\" name=\"id\" type=\"hidden\" value=\"" . $id . "\">";  //appended hidden input to keep previous data on the page.


    } else {
        echo "<img id=\"closed-image\" src=\"img/envelope-closed.png\" alt=\"email not opened\"/><br><p id=\"closed-para\">"
        . "Mail sent from <b>" . $from . "</b><br> To <b>" . $to
        . "</b><br><div id=\"color-not-read\"> Not yet opened......</div></p>"
        . "<input id=\"id1\" name=\"id\" type=\"hidden\" value=\"" . $id . "\">";  //appended hidden input to keep previous data on the page.
    }
}

Step 3. Create a PHP file with name “trackonline.php”. This will be the PHP script to log an entry in a text file also check for the duplicity.

步骤 3. 创建一个名为“trackonline.php”的 PHP 文件。这将是 PHP 脚本,用于在文本文件中记录条目并检查是否存在重复性。

<?php

if (!empty($_GET['email'])) {
    $id = $_GET['id'];
    $checkid = "Id:" . $id;
    $email = $_GET['email'];
    $sub = $_GET['subject'];
    date_default_timezone_set('Asia/Kolkata');
    $date = date('d/m/Y h:i:s a');
    $fh = fopen("email.txt", "a+"); //file handler
    $a = fgets($fh);
    $found = false; // init as false
    while (($buffer = fgets($fh)) !== false) {
        if (strpos($buffer, $checkid) !== false) {
            $found = true;
            break; // Once you find the string, you should break out the loop.
        }
    }
    if ($found == false) {
        $string = $email."opened the mail with subject:".$sub."on%".$date."% with mailId:".$id."\n";
        fwrite($fh, $string);
    }
    fclose($fh);

    //Get the http URI to the image
    $graphic_http = 'https://yourwebsite.com/blank.gif';

    //Get the filesize of the image for headers
    $filesize = filesize('blank.gif');

    //Now actually output the image requested, while disregarding if the database was affected
    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="blank.gif"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . $filesize);
    readfile($graphic_http);

    //All done, get out!
    exit;
}