Track your sent Emails using Coldfusion.

AID: 2386
  • Status: Published

2750 points

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>
                                    
1:
2:
3:
4:
5:
6:

Select allOpen 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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen 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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

Select allOpen 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" />
                                    
1:

Select allOpen 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('*.*'))#
                                    
1:

Select allOpen in new window

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

Select allOpen 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.
Asked On
2010-02-03 at 03:37:30ID2386
Tags

Coldfusion

Topic

ColdFusion Application Server

Views
2207

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top ColdFusion Server Experts

  1. Zvonko

    2,800

    0 points yesterday

    Profile
    Rank: Genius
  2. srikanthmadishetti

    2,580

    20 points yesterday

    Profile
    Rank: Guru
  3. digicidal

    2,000

    0 points yesterday

    Profile
    Rank: Guru
  4. maestropsm

    1,600

    0 points yesterday

    Profile
    Rank: Guru
  5. _agx_

    1,268

    0 points yesterday

    Profile
    Rank: Genius
  6. gdemaria

    1,064

    0 points yesterday

    Profile
    Rank: Genius
  7. brijeshchauhan

    668

    0 points yesterday

    Profile
    Rank: Guru
  8. myselfrandhawa

    250

    10 points yesterday

    Profile
    Rank: Guru

Hall Of Fame