how to track which email is open using phpmailer

58 Views Asked by At

Hello I wrote the following script for email track which user opened email from the inbox. I used the the phpmailer. While i opened my email from the inbox database doesn't update.


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
 
// load Composer's autoloader
require 'vendor/autoload.php';

$connect = new PDO("mysql:host=localhost;dbname=mailtest", "root", "");
$base_url = 'http://localhost/howtrack/index.php/'; //

$message = '';

if(isset($_POST["send"]))
{
    require 'class/class.phpmailer.php';

    $mail = new PHPMailer;
    $mail->IsSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = '587';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'oncb wvge upya gbyg';
    $mail->SMTPSecure = '';
/*  $mail->From = '[email protected]';
    $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;
}

track.php

This is the update script. While user click the inbox email database doesn't update. where is the problem. could you rewrite the script.

$connect = new PDO("mysql:host=localhost;dbname=mailtest", "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();
}

I wrote the above script. That is doesn't work perfectly.

0

There are 0 best solutions below