20

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

谁能指导我如何做到这一点?

谢谢

4

6 回答 6

28

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

PHP代码:

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

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

于 2013-12-22T12:09:27.327 回答
6

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

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

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

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

编辑:代码参考:

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

checkopen.php脚本中,获取user_id现在对应于该字段的字段,存储该用户已打开邮件。

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

因此,每当呈现此图像时,它都会调用相应的 url,因此您可以登录系统了解正在打开的邮件的状态。

于 2013-12-22T12:04:33.750 回答
3

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

于 2013-12-22T13:16:56.307 回答
2

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

这是我放入 /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;
?>

对于图像文件名,我使用了以下内容:

http://mywebsite.com/email_image/?message_id=12345

email_image 文件夹中还有一个名为“message_open_tracking.gif”的空白 1x1 gif 图像。

还可以修改 index.php 文件以使用 message_id 来将该消息标记为已读。如果查询字符串中包含其他变量,例如收件人的电子邮件地址,则这些值也可以在该 index.php 文件中使用。

非常感谢 Bennett Stone 的以下文章: http ://www.phpdevtips.com/2013/06/email-open-tracking-with-php-and-mysql/

于 2014-08-28T21:30:15.143 回答
0

有许多可能性,从客户的角度来看是不幸的,因为其中许多被垃圾邮件发送者滥用,他们想知道他们的邮件地址是否“活着”。

https://www.emailprivacytester.com列出(并测试)了最详尽的技术列表

我认为从您的角度来看,最“友善”的解决方案是处置通知标头。然而,要求一个“可靠”的解决方案违背了接受者的普遍利益,即不应该有一种方法可以可靠地观察他们违背自己意愿的行为。

于 2021-04-07T14:02:05.247 回答
-1

这是完整指南的链接

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

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

步骤 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>

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

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

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

<?php

require ('phpmailer/PHPMailerAutoload.php');
$from = "test@gmail.com";    //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.
    }
}

步骤 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;
}
于 2018-10-06T10:07:16.153 回答