<

Track your sent Emails using Coldfusion.

Published on
11,592 Points
5,592 Views
Last Modified:
Approved
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_email
------------

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" 
        from="XXXX@XX.com" 
        subject="Test tracker" 
        type="HTML">
    <img src="http://yourdomain.com/image.cfm?id=1234" />
</cfmail>

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.
<cfquery>
    insert into user_email (
        user_id,
        email,
        subject
    ) values (
        1234,
        'XXXX@XX.com',
        'Test tracker'
    );   
</cfquery>

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 --->
    <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>

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().
(http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_e-g_36.html)
#GetDirectoryFromPath(ExpandPath('*.*'))#

Open in new window

(http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_11.html)
<cfcontent type="image/jpeg" file="#imgfile#" />

Open in new window


Summary:

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.
0
Comment
0 Comments

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month