How to check email open or not open in php

We will see if the email open or not open. This is very important for a developer. If you are a developer then this code will be very useful in your coding

How do you track whether an email is open or not?

We need to use emoji to see if the email is open. Through this image, I can understand whether the images are open or not. If you want to become a developer, you can better understand the following code by floating around

If you want to see the results of your emails, the first thing you need to do is create some files that will contain your PHP code. Those of you who can send emails via PHP can skip this part and do the following part at once. Because here I will show you how to send an email. There are two ways to send an email: –

1. The default function of PHP

Example

<?php 
    $to = $_POST['email']; 
    $subject = "Email Subject"; 
    $message = 'Dear '.$_POST['name'].',<br>'; 
    $message .= "We welcome you to be part of family<br><br>";
    $message .= "Regards,<br>";
    // Always set content-type when sending HTML email 
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
    // More headers
    $headers .= 'From: <enquiry@example.com>' . "\r\n"; 
    $headers .= 'Cc: myboss@example.com' . "\r\n";
    mail($to,$subject,$message,$headers); 
?>

NEXT Example

<?php 
    ini_set( 'display_errors', 1 ); 
    error_reporting( E_ALL ); 
    $from = "test@hostinger-tutorials.com";
    $to = "test@hostinger.com"; 
    $subject = "Checking PHP mail";
    $message = "PHP mail works just fine"; 
    $headers = "From:" . $from; if(mail($to,$subject,$message, $headers)) { 
    echo "The email message was sent."; 
    } else { 
     echo "The email message was not sent."; 
    } 
?>

The first parameter in this function is to, subject, message, header. The email function is created with these parameters. If you want to know more about this, click read more.

2. Through SMPT.

If you want to know how to send an email via SMPT, click read more now and find out how to send an email via SMTP.

Now we can come to our main topic. To find out if the user is opening my email, first, you have to create a database. If you don’t know how to create a database, then click read more. Below are the codes for creating the table.

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0; 
START TRANSACTION; 
SET time_zone = "+00:00"; 
 
 CREATE TABLE `email_list` ( 
        `id` int(11) NOT NULL, 
        `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
        `send` int(11) NOT NULL,
        `open` int(11) NOT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 
        
    INSERT INTO `email_list` (`id`, `email`, `send`, `open`) VALUES 
    (1, 'beginnerwebdeveloper@gmail.com', 0, 0);


    ALTER TABLE `email_list`
      ADD PRIMARY KEY (`id`); ALTER TABLE `email_list` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
    COMMIT;

I have named the send table in the database so that we can see if our email is gone and I have named the open table to see if the user is opening it. If the user does not open the email then the default value will be 0. And if it opens, each value will become one. We will hit a page with the image that is in it. On this page, we will make the value of the email one. No one can see this code. You can see it if you want. For this you have to click on the network option next to the view-source, then you will see that we will pass an ID with the email with which we have made the value 1 in the email.

index code 
<?php 
include('db.php'); 
include('smtp/PHPMailerAutoload.php'); 
$res=mysqli_query($con,"select * from email_list where send='0'");
    if(mysqli_num_rows($res)>0){ 
        $row=mysqli_fetch_assoc($res);
        $email=$row['email']; 
        $id=$row['id']; 
        $html="Hi Hello. <img src='TRACKINGURL/track.php?id=$id' width='1px' height='1px'/>"; 
        smtp_mailer($email,'Test', $html);
        mysqli_query($con,"update email_list set send=1 where id='$id'");
     }
    function smtp_mailer($to,$subject, $msg){ $mail = new PHPMailer(); 
            $mail->IsSMTP(); $mail->SMTPAuth = true; 
            $mail->SMTPSecure = 'tls';
            $mail->Host = "smtp.gmail.com"; 
            $mail->Port = 587;
            $mail->IsHTML(true); $mail->CharSet = 'UTF-8';
            $mail->Username = "EMAIL"; 
            $mail->Password = 'PASSWORD';
            $mail->SetFrom("EMAIL"); $mail->Subject = $subject; 
            $mail->Body =$msg;
            $mail->AddAddress($to);
            $mail->SMTPOptions=array('ssl'=>array( 'verify_peer'=>false, 'verify_peer_name'=>false, 'allow_self_signed'=>false ));
            if(!$mail->Send()){ 
                echo $mail->ErrorInfo;
            }else{
                echo 'Sent';
                } 
    }
 ?>
Track page
<?php 
include('db.php'); 
if(isset($_GET['id']) && $_GET['id']>0){
     $id=mysqli_real_escape_string($con,$_GET['id']);
     mysqli_query($con,"update email_list set open=1 where id='$id'");
    //mysqli_query($con,"update email_list set open=open+1 where id='$id'"); 
} ?>

Database config

<?php
 $con=mysqli_connect('DB_HOST','DB_USER_NAME','DB_PASSWORD','DB_NAME'); 
?>

DOWNLOAD FULL SOURCE CODE

3 thoughts on “How to check email open or not open in php”

Leave a Comment