Login

Email Munger

Author:
cootetom
Posted:
January 16, 2009
Language:
Python
Version:
1.0
Tags:
filter email
Score:
0 (after 0 ratings)

Template filter to hide an email address away from any sort of email harvester type web scrapers and so keep away from spam etc.

The filter should be applied on a string which represents an email address. You can optionally give the filter a parameter which will represent the name of the resulting email href link. If no extra parameter is given the email address will be used as the href text.

{{ email|mungify:"contact me" }} or {{ email|mungify }}

The output is javascript which will write out the email href link in a way so as to not actually show the email address in the source code as plain text.

Also posted on my site.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape
import re

register = template.Library()

@register.filter
@stringfilter
def mungify(email, text=None, autoescape=None):
    text = text or email
    
    if autoescape:
        email = conditional_escape(email)
        text = conditional_escape(text)

    emailArrayContent = ''
    textArrayContent = ''
    r = lambda c: '"' + str(ord(c)) + '",'

    for c in email: emailArrayContent += r(c)
    for c in text: textArrayContent += r(c)

    result = """<script>
                var _tyjsdf = [%s], _qplmks = [%s];
                document.write('<a href="&#x6d;&#97;&#105;&#x6c;&#000116;&#111;&#x3a;');
                for(_i=0;_i<_tyjsdf.length;_i++){document.write('&#'+_tyjsdf[_i]+';');}
                document.write('">');
                for(_i=0;_i<_qplmks.length;_i++){document.write('&#'+_qplmks[_i]+';');}
                document.write('</a>');
                </script>""" % (re.sub(r',$', '', emailArrayContent),
                                re.sub(r',$', '', textArrayContent))
    
    return mark_safe(result)

mungify.needs_autoescape = True

More like this

  1. Email obfuscation filter using ROT13 by worksology 6 years, 3 months ago
  2. SMTPConnection with Return-Path by sgb 7 years, 3 months ago
  3. Simple e-mail template tag by dchandek 7 years, 3 months ago
  4. Soft hyphenation (&shy;) template filter using PyHyphen by dokterbob 6 years, 3 months ago
  5. entity encoded email-address by emna 6 years, 3 months ago

Comments

Please login first before commenting.