Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ? If yes, then this article is for you !
First we need a table user_email with columns user_id , email , subject , opened , dateopened to insert a record when we send out an email with the email details and then update the same record when email is opened with current date and time and opened flag to true.
user_id - userid of the user
email - email of the user
subject - store subject of the email
opened - set the flag true after the email is read, default is false
dateopened - insert date and time when the email is opened
Please note the value of the
srcattribute of the
imgtag is pointing to a .cfm file and not to a physical image file. In addition, the
idURL parameter is concatenated to the end of the URL for image.cfm in order to uniquely identify each user/email which will be useful in tracking later.
After sending email Insert record in to user_email table with the details of email sent so that we can update the same record and set the opened flag to true when the email is opened.
<cfif structkeyexists(url,"id")> <cfset tm = dateformat(now(),"mm/dd/yyyy") &" "& timeformat(now(),"hh:mm:ss") /> <!--- update users table with current date and time, and set flag to true of the User id equal to Url.id ---> <cfquery> update user_email set dateopened = #tm#, opened = 1 where userid = #url.id# and subject='Test tracker' and coalesce(opened, 0) = 0; </cfquery> <cfset imgfile = "#GetDirectoryFromPath(ExpandPath('*.*'))#h1.jpg" /> <cfcontent type="image/jpeg" file="#imgfile#" /></cfif>
As stated earlier, the
image.cfmis the actual file which will be called when the user opens the email.
In this file, as shown in the code above, we update the user_email table's dateopened column with the tm variable, which has current date and time value, and set the opened flag to true, which will help us to track easily, of the respective userid, which is available in the form of the URL variable in this file and the subject of the email which we used while sending out email.
Now, we are left with displaying the image, which we can achieve in 2 ways: cflocation or cfcontent.
cflocation: redirects from image.cfm to the actual image file h1.jpg.