With the ever-increasing annoyance of spiders and bots getting our e-mails onto their lists for free Viagra, it's almost a death sentence putting your e-mail up on a website. What we have here is a small CFC that will take your e-mail address, scramble into ASCII code, and return to the browser exactly how you entered it. The only difference is that when bots scan your source code, they won't find an e-mail formatted string. Instead, they will find a large amount of ampersands, hash marks and semi-colons. While this may not be the silver bullet forever, at least it's a good start.

Plus, this code is easily reused. By creating a globally accessible variable (REQUEST scope comes to mind), you can use the scrambled version numerous times if need be.

How It Works
It's quite simple in how it scrambles your e-mail. When you pass an e-mail into the CFC, it replaces every character in your e-mail with its ASCII equivalent. So, while you see foo@bar.com, viewing your browser source code will reveal <b>&#102;&#111;&#111;&#64;&#98;&#97;&#114;&#46;&#99;&#111;&#109;</b>. This allows the user to see your e-mail as letters, numbers and characters, but prevents any bots from pharming your e-mail, as they look for specifc patterns in the text to recognize a basic e-mail format.

The Code
You need two files, one for holding the CFC, and any other to call it. First, the calling block of code.

<cfinvoke component="email_scramble" method="scramble" returnvariable="variables.scrambledEmail">
    <cfinvokeargument name="email" value="#variables.email#" />
</cfinvoke>

Keeping files and variables simply named, I have email_scramble.cfc being invoked, with the variables.email passing in the e-mail address you wish to scramble. Place #variables.email# around output tags to render it to the browser.

And here is the CFC itself:

<cfcomponent displayname="Scramble Emails" hint="Takes passed emails and scrambles them to hide from bots">
    <cffunction name="scramble" access="public" output="false" hint="Scrambles passed email address" returntype="string">
        <cfargument name="email" type="string" default="" hint="Email passed in" required="yes">

        <cfset email = replace(email, "@", "&###asc('@')#;", "all") />
        <cfset email = replace(email, ".", "&###asc('.')#;", "all") />
        <cfset email = replace(email, "_", "&###asc('_')#;", "all") />
        <cfset email = replace(email, "-", "&###asc('-')#;", "all") />
        <cfset email = replace(email, "?", "&###asc('?')#;", "all") />
        <cfset email = replace(email, "!", "&###asc('!')#;", "all") />
        <cfset email = replace(email, "=", "&###asc('=')#;", "all") />
        <cfset email = replace(email, "/", "&###asc('/')#;", "all") />
        <cfset email = replace(email, ":", "&###asc(':')#;", "all") />
        <cfset email = replace(email, ",", "&###asc(',')#;", "all") />
        <cfset email = replace(email, "+", "&###asc('+')#;", "all") />
        <cfset email = replace(email, "*", "&###asc('*')#;", "all") />
        <cfset email = replace(email, "(", "&###asc('(')#;", "all") />
        <cfset email = replace(email, ")", "&###asc(')')#;", "all") />
        <cfset email = replace(email, "<", "&###asc('<')#;", "all") />
        <cfset email = replace(email, ">", "&###asc('>')#;", "all") />
        <cfset email = replace(email, "a", "&###asc('a')#;", "all") />
        <cfset email = replace(email, "b", "&###asc('b')#;", "all") />
        <cfset email = replace(email, "c", "&###asc('c')#;", "all") />
        <cfset email = replace(email, "d", "&###asc('d')#;", "all") />
        <cfset email = replace(email, "e", "&###asc('e')#;", "all") />
        <cfset email = replace(email, "f", "&###asc('f')#;", "all") />
        <cfset email = replace(email, "g", "&###asc('g')#;", "all") />
        <cfset email = replace(email, "h", "&###asc('h')#;", "all") />
        <cfset email = replace(email, "i", "&###asc('i')#;", "all") />
        <cfset email = replace(email, "j", "&###asc('j')#;", "all") />
        <cfset email = replace(email, "k", "&###asc('k')#;", "all") />
        <cfset email = replace(email, "l", "&###asc('l')#;", "all") />
        <cfset email = replace(email, "m", "&###asc('m')#;", "all") />
        <cfset email = replace(email, "n", "&###asc('n')#;", "all") />
        <cfset email = replace(email, "o", "&###asc('o')#;", "all") />
        <cfset email = replace(email, "p", "&###asc('p')#;", "all") />
        <cfset email = replace(email, "q", "&###asc('q')#;", "all") />
        <cfset email = replace(email, "r", "&###asc('r')#;", "all") />
        <cfset email = replace(email, "s", "&###asc('s')#;", "all") />
        <cfset email = replace(email, "t", "&###asc('t')#;", "all") />
        <cfset email = replace(email, "u", "&###asc('u')#;", "all") />
        <cfset email = replace(email, "v", "&###asc('v')#;", "all") />
        <cfset email = replace(email, "w", "&###asc('w')#;", "all") />
        <cfset email = replace(email, "x", "&###asc('x')#;", "all") />
        <cfset email = replace(email, "y", "&###asc('y')#;", "all") />
        <cfset email = replace(email, "z", "&###asc('z')#;", "all") />
        <cfset email = replace(email, "A", "&###asc('A')#;", "all") />
        <cfset email = replace(email, "B", "&###asc('B')#;", "all") />
        <cfset email = replace(email, "C", "&###asc('C')#;", "all") />
        <cfset email = replace(email, "D", "&###asc('D')#;", "all") />
        <cfset email = replace(email, "E", "&###asc('E')#;", "all") />
        <cfset email = replace(email, "F", "&###asc('F')#;", "all") />
        <cfset email = replace(email, "G", "&###asc('G')#;", "all") />
        <cfset email = replace(email, "H", "&###asc('H')#;", "all") />
        <cfset email = replace(email, "I", "&###asc('I')#;", "all") />
        <cfset email = replace(email, "J", "&###asc('J')#;", "all") />
        <cfset email = replace(email, "K", "&###asc('K')#;", "all") />
        <cfset email = replace(email, "L", "&###asc('L')#;", "all") />
        <cfset email = replace(email, "M", "&###asc('m')#;", "all") />
        <cfset email = replace(email, "N", "&###asc('N')#;", "all") />
        <cfset email = replace(email, "O", "&###asc('O')#;", "all") />
        <cfset email = replace(email, "P", "&###asc('P')#;", "all") />
        <cfset email = replace(email, "Q", "&###asc('Q')#;", "all") />
        <cfset email = replace(email, "R", "&###asc('R')#;", "all") />
        <cfset email = replace(email, "S", "&###asc('S')#;", "all") />
        <cfset email = replace(email, "T", "&###asc('T')#;", "all") />
        <cfset email = replace(email, "U", "&###asc('U')#;", "all") />
        <cfset email = replace(email, "V", "&###asc('V')#;", "all") />
        <cfset email = replace(email, "W", "&###asc('W')#;", "all") />
        <cfset email = replace(email, "X", "&###asc('X')#;", "all") />
        <cfset email = replace(email, "Y", "&###asc('Y')#;", "all") />
        <cfset email = replace(email, "Z", "&###asc('Z')#;", "all") />
        <cfreturn email />
    </cffunction>
</cfcomponent>

Taking the e-mail passed in, it replaces all occurrences of both uppercase, lowercase letters and a handful of allowed special characters (replacing numbers 0-9 was creating errors, so help is appreciated). Then, it returns the hash-and-ampersand full string back to the calling page. There, you may do with it as you wish.

Conclusion
This CFC developed out of a JavaScript function of similar effect, but decided to make a server-side version that isn't browser-option dependent. While I was unable to get numbers to render, the above should cover 95% of all e-mails easily, and at the very least, the ampersand and dot will be converted.

About This Tutorial
Author: David Patricola
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX,CFMX7
Total Views: 95,668
Submission Date: July 09, 2006
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • Here is how to make this work and include numbers at the same time. Much simpler this way and handles the case of the number ascii codes looping over themselves. Much much simpler....

  • "" *Dude* - use a loop: ret=' loop i from 1 to len(email) ret=ret + '&#'+asc(stringGetAt(i)) Much less typing, much less error prone.

Advertisement

Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.