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();
}
?>
Tenku
ReplyDeletethis file is curputed
ReplyDeletekindly code bulk emali using php mailler and tracking that emails also.
ReplyDeleteit's not work for me can u help me
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSir don't a create a SQL, please send me file SQL (aqibjaved25330@gmail.com)email.
ReplyDeletewhat if the mail is opened from sent item? does it track the info?
ReplyDeletecan you send me the sql file...thanks ...(sadiqadetunji2016@gmail.com)
ReplyDeleteemail_track.php is vulnerable to sql injections, please do:
ReplyDelete$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 = ?
AND email_status = 'no'
";
$statement = $connect->prepare($query);
$statement->execute(array($_GET["code"]));
thanks for your info
Deletewhen i send for the first then it gets automatically opened.Please help
ReplyDeleteHi i am not getting download link yet. sir please send me link at rawat_pradeep@ymail.com. Thanks
ReplyDeleteHi i am not getting download link yet. sir please send me link at rawat_pradeep@ymail.com. Thanks
ReplyDeleteSir please send me file SQL (sobujas745@gmail.com)
ReplyDeleteThank you for sharing this, but the problem is that this method doesn't work for gmail accounts.
ReplyDeleteCan you help me with it, please?
this scripts currently not working in 2020. please tell me other ways
ReplyDeleteShere the sql file plese
ReplyDeletenot working
ReplyDeleteemail_status automatically changes yes befor open email ???
ReplyDeletesql------
CREATE TABLE IF NOT EXISTS `email_data` (
`email_id` int(11) NOT NULL AUTO_INCREMENT,
`email_subject` varchar(255) NOT NULL,
`email_body` text NOT NULL,
`email_address` varchar(255) NOT NULL,
`email_track_code` decimal(10,0) NOT NULL,
`email_status` varchar(100) NOT NULL,
`email_open_datetime` datetime NOT NULL,
PRIMARY KEY (`email_id`)
) ;
php--------
$no = "no";
$data = array(
':email_subject' => $_POST["email_subject"],
':email_body' => $_POST["email_body"],
':email_address' => $_POST["receiver_email"],
':email_status' => $no,
':email_track_code' => $track_code
);
$query = "
INSERT INTO email_data
(email_subject, email_body, email_address, email_track_code, email_status) VALUES
(:email_subject, :email_body, :email_address, :email_track_code, :email_status)
";
Hello I have tried this code. Its not working for me. Please help
ReplyDelete