Skip to main content

How know recipient read the email or newsletter?

How know recipient read the email or newsletter?

Sometimes we send the newsletters or emails to lot of peoples and
we want to know how many of them read it. Normally the websites like
Gmail, Yahoo, Hotmail don’t provide any facility for this purpose. But
we can know that by sending HTML emails.

HTML Emails – HTML emails are emails which can contain HTML tags in email body. Like <table>, <tr>, <td>, etc.

The main thing here is we need to send some information to our server
when user open the email. So for this, we can place an image in email
body and when user open this email the browser send request to our
webserver for this image. We can also achieve this by adding javascript code or hidden iframe
but normally email clients don’t support javascript code and iframes in
email body and they strips out this unwanted code form email body. So,
the best way to achieve this is by using images.

<?php
/*send_newsletter.php*/
 
$newsletterid = 25;
 
// array of recipients
$recipients = array('test1@yahoo.com', 'test2@yahoo.com', 'test3@yahoo.com', 'test4@gmail.com', 'test5@gmail.com', 
	'test6@hotmail.com', 'test7@hotmail.com', 'test8@aol.com', 'test9@aol.com', 'test10@yahoo.com');
 
// subject
$subject = 'Newsletter for April';
 
// message
$message = "
<html>
<head>
 <title>Newsletter for April</title>
</head>
<body>
 <p>This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. 
 This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. 
 This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. </p>
 <img src='http://www.akchauhan.com/newsletter_read.php?newsletterid={$newsletterid}' height='1' width='1' /><br/><br/>
 Thanks,<br/>
 www.akchauhan.com
</body>
</html>";
 
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= 'From: AKChauhan Blog <anil@akchauhan.com>' . "rn";
 
foreach($recipients as $to) {
	// Mail it
	mail($to, $subject, $message, $headers);
}
?>

I have used a variable $newsletterid to track newsletter. We can also send some extra information in query string as per our requirement.
Like

http://www.akchauhan.com/newsletter_read.php?newsletterid=$newsletterid&userid=$userid

Note: Here I have used mail()
function only to make demonstration simple. mail() function is not
suitable for sending large number of email in loop. Better we use PEAR::Mail or PEAR::Mail_Queue packages for sending large number of email.

<?php
/*newsletter_read.php*/
 
$newsletterid = $_GET['newsletterid'];
/*Write some code to update your read counter in your database. Or, do whatever you want to 
do when user open your newsletter.*/
mysql_query('UPDATE info SET newsletter_count = newsletter_count + 1 WHERE newsletterid = ' . $newsletterid);
 
// We'll be outputting a JPEG
header('Content-Type: image/jpeg');
 
$im = @imagecreatefromjpeg('images/one_pixel_small_white_image.jpg');
 
imagejpeg($img);
imagedestroy($img);
?>

In above code, first we updated our newsletter read counter in
database. We can do other things also like send an email to admin etc.
as per requirement. Then we set the Content-Type to image/jpeg
as we are going to send a jpeg image. So, we need to tell the browser
about the content type of our response. Then, we created a 1 pixel white
image usng GD library and outputted it to browser using imagejpeg() function.

, , ,