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

时间:2013-12-22 12:00:57

标签: php mass-emails campaign-monitor

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

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

由于

6 个答案:

答案 0 :(得分:15)

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

PHP代码:

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

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

答案 1 :(得分:4)

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

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

由于gmail始终开始显示图像,但是将从自己的服务器托管它们,您可以了解邮件是否已打开,但您可能无法跟踪正确的IP。在这里查看一些参考:Effect of gmail caching and showing images by default

您可以了解mailchimp的用途:MailChimp Working

编辑: 代码参考:

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

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

发送邮件时,请务必在每次发送邮件时增加user_id字段。

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

答案 2 :(得分:2)

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

我非常反对嵌入允许用户跟踪的小HTML片段。

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

Here is link to complete guide

[[先决条件:]] 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脚本有两个用途:

a。使用PHP邮件库发送邮件。

b。阅读日志文件(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;
}

答案 5 :(得分:0)

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

最详尽的技术列表由 https://www.emailprivacytester.com

列出(并测试)

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

相关问题