Thursday, 23 August 2018

How to Track Email Open or not using PHP



This post on email tracker in php and here you can find how to track an email is open or not by receiver. Here we will make PHP script for tracking promotional or email marketing email has been open or not by receiver. This things we will make by putting PHP script in HTML tag and this tag we will put into message body. So when receiver has been open an email then img tag will called PHP script for update email status with date and time.

Why we want to Track an email?


After send of lots of email, then we want to get result from that sending of an email, so how can we get result, so result we will get when email receiver has been open our email or not which we have send them. This things we can done by ask for email receipt to the receiver while send of email with many email SMTP service provider like Gmail, Yahoo, Outlook and many more other. But this things only work if we have request.

So if receiver has been ignores our receipt request then we will not be get any information regarding email has been open or not. This type of things can be easily done by PHP script If your website has been build with PHP. Because we can make email tracker easily in PHP. In this post you can find complete source code on email tracking with PHP and HTML Image. By using this two things we can create email open tracking with PHP.

This simple PHP script will allows you to track open or not HTML emails. This feature you can also add in your web application also if you have continuously send email to your customer for reach. Below you can find step by step source code for email open tracking PHP script.










Source Code


index.php



<?php

//index.php

$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
$base_url = 'http://localhost/tutorial/how-to-track-email-open-or-not-using-php/'; //

$message = '';

if(isset($_POST["send"]))
{
 require 'class/class.phpmailer.php';
 $mail = new PHPMailer;
 $mail->IsSMTP();
 $mail->Host = 'smtpout.secureserver.net';
 $mail->Port = '80';
 $mail->SMTPAuth = true;
 $mail->Username = 'xxxxxxxxxx';
 $mail->Password = 'xxxxxxxxxx';
 $mail->SMTPSecure = '';
 $mail->From = 'info@webslesson.info';
 $mail->FromName = 'Webslesson.info';
 $mail->AddAddress($_POST["receiver_email"]);
 $mail->WordWrap = 50;
 $mail->IsHTML(true);
 $mail->Subject = $_POST['email_subject'];

 $track_code = md5(rand());

 $message_body = $_POST['email_body'];

 $message_body .= '<img src="'.$base_url.'email_track.php?code='.$track_code.'" width="1" height="1" />';
 $mail->Body = $message_body;

 if($mail->Send())
 {
  $data = array(
   ':email_subject'   =>  $_POST["email_subject"],
   ':email_body'    =>  $_POST["email_body"],
   ':email_address'   =>  $_POST["receiver_email"],
   ':email_track_code'   =>  $track_code
  );
  $query = "
  INSERT INTO email_data 
  (email_subject, email_body, email_address, email_track_code) VALUES 
  (:email_subject, :email_body, :email_address, :email_track_code)
  ";

  $statement = $connect->prepare($query);
  if($statement->execute($data))
  {
   $message = '<label class="text-success">Email Send Successfully</label>';
  }
 }
 else
 {
  $message = '<label class="text-danger">Email Send Successfully</label>';
 }

}

function fetch_email_track_data($connect)
{
 $query = "SELECT * FROM email_data ORDER BY email_id";
 $statement = $connect->prepare($query);
 $statement->execute();
 $result = $statement->fetchAll();
 $total_row = $statement->rowCount();
 $output = '
 <div class="table-responsive">
  <table class="table table-bordered table-striped">
   <tr>
    <th width="25%">Email</th>
    <th width="45%">Subject</th>
    <th width="10%">Status</th>
    <th width="20%">Open Datetime</th>
   </tr>
 ';
 if($total_row > 0)
 {
  foreach($result as $row)
  {
   $status = '';
   if($row['email_status'] == 'yes')
   {
    $status = '<span class="label label-success">Open</span>';
   }
   else
   {
    $status = '<span class="label label-danger">Not Open</span>';
   }
   $output .= '
    <tr>
     <td>'.$row["email_address"].'</td>
     <td>'.$row["email_subject"].'</td>
     <td>'.$status.'</td>
     <td>'.$row["email_open_datetime"].'</td>
    </tr>
   ';
  }
 }
 else
 {
  $output .= '
  <tr>
   <td colspan="4" align="center">No Email Send Data Found</td>
  </tr>
  ';
 }
 $output .= '</table>';
 return $output;
}


?>
<!DOCTYPE html>
<html>
 <head>
  <title>How to Track Email Open or not using PHP</title>
  <script src="jquery.min.js"></script>
  <link rel="stylesheet" href="bootstrap.min.css" />
  <script src="bootstrap.min.js"></script>
 </head>
 <body>
  <br />
  <div class="container">
   <h3 align="center">How to Track Email Open or not using PHP</h3>
   <br />
   <?php
   
   echo $message;

   ?>
   <form method="post">
    <div class="form-group">
     <label>Enter Email Subject</label>
     <input type="text" name="email_subject" class="form-control" required />
    </div>
    <div class="form-group">
     <label>Enter Receiver Email</label>
     <input type="email" name="receiver_email" class="form-control" required />
    </div>
    <div class="form-group">
     <label>Enter Email Body</label>
     <textarea name="email_body" required rows="5" class="form-control"></textarea>
    </div>
    <div class="form-group">
     <input type="submit" name="send" class="btn btn-info" value="Send Email" />
    </div>
   </form>
   
   <br />
   <h4 align="center">Sending Email Open or not status</h4>
   <?php 
   
   echo fetch_email_track_data($connect);

   ?>
  </div>
  <br />
  <br />
 </body>
</html>


email_track.php



<?php

//email_track.php

$connect = new PDO("mysql:host=localhost;dbname=testing", "root","");

if(isset($_GET["code"]))
{
 $query = "
 UPDATE email_data 
 SET email_status = 'yes', email_open_datetime = '".date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa')))."' 
 WHERE email_track_code = '".$_GET["code"]."' 
 AND email_status = 'no'
 ";
 $statement = $connect->prepare($query);
 $statement->execute();
}

?>





2 comments:

  1. kindly code bulk emali using php mailler and tracking that emails also.

    ReplyDelete