我正在使用PHP发送电子邮件以跟踪谁打开了我的电子邮件。我的代码有问题,因为当我发送电子邮件给gmail并第一次打开电子邮件时,它将多次更新mysql数据库中的值。
示例:数据库中的值显示为0,所以当我第一次打开电子邮件时,它将多次更新,因为该值将显示为2。每次更新时,应每次更新一次我打开电子邮件以更新数据库中的值。
OpenMail.php:
<?php
header("Content-Type: image/jpeg");
readfile("../Images/track.jpeg");
//Connect to the database
include('config.php');
$id = $_GET['id'];
$tracking_sql = mysqli_fetch_assoc(mysqli_query($link, "SELECT id, email_id, email, isRead FROM sent WHERE id = '$id'"));
$email_id = $tracking_sql['email_id'];
$email = $tracking_sql['email'];
$opened = $tracking_sql['isRead'];
$date = date('Y-m-d H:i:s');
mysqli_query($link,"UPDATE `sent` SET `isRead`=`isRead`+1,readDateTIme = '$date' WHERE id= '$id' and `readDateTIme` NOT BETWEEN DATE_SUB(NOW() , INTERVAL 3 SECOND) AND NOW()");
//mysqli_query($link, "UPDATE sent SET opened = opened + 1, datetime = '$date' WHERE id = '$sent_id'");
//close the connection
mysqli_close($link);
?>
SendMail.php:
<?php
require_once "Mail.php";
require_once "Mail/mime.php";
require_once('Mail/IMAPv2.php');
//Connect to the database
include('config.php');
$inbox = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX';
$spam = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX.spam';
$mailserver = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX.Sent';
if (isset($_POST['send_to']))
{
$from = "Chris <chris@domain.com>";
$to_email = $_POST['send_to'];
$to = $firstname . " <$to_email>";
$subject = $_POST['emailsubject'];
$message = $_POST['emailbody'];
$smtp_hostname = "smtp.domain.com";
$port = "587";
$username = $_POST['username'];
$password = $_POST['password'];
$messageID = sprintf("<%s.%s@%s>",
base_convert(microtime(), 10, 36),
base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
'domain.com');
$date = date('Y-m-d H:i:s');
$emailID = '';
$sent_db = "SELECT email_id FROM sent WHERE email_id = '$messageID'";
$sent_result = mysqli_query($link, $sent_db) or die($link->error);
if(mysqli_num_rows($sent_result) == 0)
{
$sent_db1 = "INSERT INTO sent (id, email_id, email, sendDateTime, isRead, readDateTIme) VALUES ('', '$messageID', '$to', '$date', '0', '0000-00-00 00:00:00')";
if (mysqli_query($link, $sent_db1)) {
$emailID = mysqli_insert_id($link);
}
}
$message .= "<img src=\"http://test.domain.com/project/track/Images/signature.gif?id=".$emailID."&etc=".time()." \" style=\"width: 0; max-height:0; overflow:hidden; \">";
$headers = array ('From' => $from,
'To' => $to, 'Subject' => $subject,
'Reply-To' => $from,
'Content-Type' => 'Content-Type: text/plain; charset=UTF-8',
'MIME-Version' => '1.0',
'Received' => 'from smtp.domain.com',
'Date' => date("r"),
'Message-ID' => sprintf("<%s.%s@%s>",
base_convert(microtime(), 10, 36),
base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
'domain.com'));
$params = array ('host' => $host,
'port' => $port,
'auth' => 'PLAIN', // Note 1
'socket_options' => array('ssl' => array('verify_peer_name' => false, 'verify_peer' => false)), // Note 2
'username' => $username,
'password' => $password);
$mime_params = array(
'text_encoding' => '7bit',
'text_charset' => 'UTF-8',
'html_charset' => 'UTF-8',
'head_charset' => 'UTF-8'
);
$crlf = "\r\n";
$mime = new Mail_mime(array('eol' => $crlf));
$body = $message;
$mime->setTXTBody($body);
$mime->setHTMLBody($body);
$body = $mime->get($mime_params);
$headers = $mime->headers($headers);
$smtp = Mail::factory ('smtp', $params);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
{
echo("<p>" . $mail->getMessage() . "</p>");
}
else
{
//echo("<p>Email has been sent!</p>");
$mailbox = imap_open($mailserver, $username, $password);
imap_append($mailbox, $mailserver,
"From: ".$from."r\n".
"To: ".$to."\r\n".
"Subject: ".$subject."\r\n".
"Date: ".date("r", strtotime("now"))."\r\n".
"Message-ID: ".$messageID."\r\n".
"MIME-Version: 1 \r\n".
"Content-Type: text/html;\r\n\tcharset=\"ISO-8859-1\"\r\n".
"Content-Transfer-Encoding: 7bit \r\n".
"\r\n\r\n".
$message.
"\r\n",
"\\Seen"
);
$inbox = imap_open($inbox, $username, $password) or die("Can't connect: " . imap_last_error());
$spam = imap_open($spam, $username, $password) or die("Can't connect: " . imap_last_error());
$total_inbox_unread = count(imap_search($inbox, 'UNSEEN'));
$total_spam_unread = count(imap_search($spam, 'UNSEEN'));
// close mail connection.
imap_close($mailbox);
imap_close($inbox);
imap_close($spam);
$response = array("total_inbox"=>$total_inbox_unread,"total_spam"=>$total_spam_unread,"success"=>"successfully");
echo json_encode($response);
}
}
// Close connection
mysqli_close($link);
?>
我尝试使用NOT BETWEEN DATE_SUB(NOW() , INTERVAL 3 SECOND) AND NOW()
作为计时器来避免值被多次更新,但并没有帮助,因为当我打开电子邮件时,值将被多次更新。 / p>
我期望实现的是,当我发送电子邮件并首次使用gmail打开电子邮件时,我只希望一次更新数据库中的值,而不能多次更新,除非再次打开电子邮件时因此它将更新数据库中的值。
能否请您举一个例子,说明我每次打开电子邮件时都如何打开电子邮件以更新数据库中的值?
谢谢。