Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Check who had read sent email?

Tags:

php

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:

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.

And I only want to solve this issue using PHP5.x.x / javascript, no other software or third party tool. Any help..?

like image 494
PHP Ferrari Avatar asked Jan 05 '11 12:01

PHP Ferrari


2 Answers

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:

$mail->ConfirmReadingTo = '[email protected]';

but it's the same as the Disposition-Notification-To method above.

like image 69
Populus Avatar answered Oct 07 '22 18:10

Populus


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't rejected by my own PHP 5.4.30 web server.

Here is the code that I put into an index.php file within an /email_image/ subdirectory:

<?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".

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.

Many thanks to Bennett Stone for the following article: http://www.phpdevtips.com/2013/06/email-open-tracking-with-php-and-mysql/

like image 25
Bryce Morrison Avatar answered Oct 07 '22 19:10

Bryce Morrison