<

[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x

Track your sent Emails using Coldfusion.

Published on
11,335 Points
5,335 Views
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_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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Join & Write a Comment

This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month