Track your sent Emails using Coldfusion.

Published on
11,490 Points
Last Modified:
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

Example Email
<cfmail to="XXXX@XX.com" 
        subject="Test tracker" 
    <img src="http://yourdomain.com/image.cfm?id=1234" />

Open in new window

Please note the value of the src attribute of the img tag is pointing to a .cfm file and not to a physical image file.  In addition, the id URL 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.
    insert into user_email (
    ) values (
        'Test tracker'

Open in new window

The image.cfm file code
<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 --->
        update user_email 
        set dateopened = #tm#, opened = 1
        where userid = #url.id# 
            and subject='Test tracker' 
            and coalesce(opened, 0) = 0;

    <cfset imgfile = "#GetDirectoryFromPath(ExpandPath('*.*'))#h1.jpg" />
    <cfcontent type="image/jpeg" file="#imgfile#" />

Open in new window

As stated earlier, the image.cfm is 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.
<cflocation url="h1.jpg" addtoken="false" />

Open in new window

cfcontent: displays the h1.jpg image directly as response from image.cfm.
Get physical directory required by cfcontent tag using GetDirectoryFromPath().

Open in new window

<cfcontent type="image/jpeg" file="#imgfile#" />

Open in new window


This simple few lines of code in ColdFusion will help us in tracking the emails sent from our applications.

Known Issues:
Most, if not ALL, email clients block images by default.
Some users may not actually open email until off-line (no Internet) or delete without opening, even if images are enabled.

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Join & Write a Comment

This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month