Django Template Tag to protect the E-mail address

E-mail SecurityHere 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 :

Demo

Hope this template tag will help you to secure your e-mail addresses from bots and spider.

Lets have a spam free web.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Other Posts which you may like:

7 responses to “Django Template Tag to protect the E-mail address”

  1. niks

    thx mate… m looking for this kinda template tag for a long….

  2. sepp

    I can do that with one line of javascript :)

  3. Amit Upadhyay

    What! no demo?

  4. Howtoblogformoney

    Nice share. Thanks

  5. Django 101 | KomunitasWeb

    [...] Django Template Tag to protect the E-mail address (February 2010) [...]

Leave a Reply

Google Custom Search