Here is a Django Template tag which you can use to protect the E-mail address on your website against bots or spiders that index or harvest E-mail addresses. It uses a substitution cipher with a different key for every page load.
This template tag encrypts the email address and generates a equivalent Javascript code that can decrypt it. This Javascript produce an email link when it runs on browser. As most bots and spider don't executes Javascript they wouldn't be able to extract mail addresses. While a visitor of your web page will not notice that you used this script as long as he/she has javascript enabled.
A normal visitor with a javascript enabled browser will find no difference. The visitor with non-javascript browser will see “[javascript protected email address]” in stead of the E-mail address.
Template Tag Code
class EncryptEmail(template.Node): def __init__(self, context_var): self.context_var = template.Variable(context_var)# context_var def render(self, context): import random email_address = self.context_var.resolve(context) character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz' char_list = list(character_set) random.shuffle(char_list) key = ''.join(char_list) cipher_text = '' id = 'e' + str(random.randrange(1,999999999)) for a in email_address: cipher_text += key[ character_set.find(a) ] script = 'var a="'+key+'";var b=a.split("").sort().join("");var c="'+cipher_text+'";var d="";' script += 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));' script += 'document.getElementById("'+id+'").innerHTML="<a href=\\"mailto:"+d+"\\">"+d+"</a>"' script = "eval(\""+ script.replace("\\","\\\\").replace('"','\\"') + "\")" script = '<script type="text/javascript">/*<![CDATA[*/'+script+'/*]]>*/</script>' return '<span id="'+ id + '">[javascript protected email address]</span>'+ script def encrypt_email(parser, token): """ {% encrypt_email user.email %} """ tokens = token.contents.split() if len(tokens)!=2: raise template.TemplateSyntaxError("%r tag accept two argument" % tokens[0]) return EncryptEmail(tokens[1]) register.tag('encrypt_email', encrypt_email)
Usage
{% encrypt_email user.email %}
The javascript code it generates is something like this:
<span id="e857606962">[javascript protected email address]</span> <script type="text/javascript"> /*<![CDATA[*/eval("var a=\"cnel8gMuoCjaYRDkv7p+ErHI5qL2x-O3Q@GdtbWKUwT9VfZSN_J04.P1hiFAXz6Bmys\";var b=a.split(\"\").sort().join(\"\");var c=\"AXzVfPXCRgMAhw9fAe91.\";var d=\"\";for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));document.getElementById(\"e857606962\").innerHTML=\"<a href=\\\"mailto:\"+d+\"\\\">\"+d+\"</a>\"")/*]]>*/ </script>
Demo
For demo visit :
Hope this template tag will help you to secure your e-mail addresses from bots and spider.
Lets have a spam free web.


thx mate… m looking for this kinda template tag for a long….
I can do that with one line of javascript
please share your implementation with others as well.
What! no demo?
Amit you can see the demo @ http://apps.nitinh.com/encrypt-email/
Nice share. Thanks
[...] Django Template Tag to protect the E-mail address (February 2010) [...]