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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 | ### models.py:
from django.db import models
from django.contrib.sites.models import Site
class Email(models.Model):
"""Model to store e-mail addresses linked to from the site."""
email = models.EmailField(unique=True)
def __str__(self):
return self.email
def get_absolute_url(self):
return '/email/%s/' % self.id
class Admin:
pass
### views.py:
from django import newforms as forms
from django.core.mail import send_mail
from django.shortcuts import get_object_or_404, render_to_response
from models import Email
class EmailForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField('E-mail address')
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea())
cc_myself = forms.BooleanField(label='Send a copy of my message to me.', required=False)
def email_form(request, email_id):
recipient = get_object_or_404(Email, pk=email_id)
if request.method == 'POST':
newdata = request.POST.copy()
form = EmailForm(data=newdata)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
cc_myself = form.cleaned_data['cc_myself']
sender = "%s <%s>" % ( name, email )
send_mail(subject, message, sender, [recipient.email,], fail_silently=False)
if cc_myself:
cc_subject = "Copy of %s" % subject
send_mail(cc_subject, message, sender, [sender,], fail_silently=False)
return render_to_response('email_links/thanks.html', { 'email': recipient.email })
else:
form = EmailForm()
return render_to_response('email_links/email_form.html', { 'form': form, 'email': recipient.email })
### templatetags/email_extras.py:
from django.template import Library
from django.contrib.sites.models import Site
from email_links.models import Email
register = Library()
def mask_email(email):
"""
Mask an email address.
Credit to jkocherhans (http://www.djangosnippets.org/snippets/79/)
"""
name, domain = email.split('@')
if len(name) > 5:
# show the first 3 characters
masked_name = name[:3]
else:
# just use the 1st character
masked_name = name[0]
return "%s...@%s" % ( masked_name, domain )
register.filter('mask_email', mask_email)
def email_link(email, label=None):
"""
Returns an e-mail link, with an optional label.
Examples:
{% email_link email label %}
{% email_link email %}
"""
email_object = Email.objects.get_or_create(email=email)[0]
if label is None:
label = mask_email(email)
return '<a href="http://%s%s">%s</a>' % ( Site.objects.get_current().domain, email_object.get_absolute_url(), label )
register.simple_tag(email_link)
### templates/email_links/email_form.html:
"""
<html>
<body>
<form method="post" action=".">
<p>Send an e-mail to {{ email|mask_email }}</p>
<table>
{{ form }}
<tr><td colspan="2" align="center"><input type="submit" name="send" value="Send e-mail" /></td></tr>
</table>
</form>
</body>
</html>
"""
### templates/email_links/thanks.html:
"""
<html>
<body>
<h2>Thanks!!</h2>
</body>
</html>
"""
|
Comments
Very nice!
It has 1 drawback though, is that e-mail addresses can be harvested by spambots. The masking introduced here only masks the e-mails for the visual eye of the user, but view-source-ing the page would reveal the address.
One way to workaround this is to base64 encode it in the link, and decode it later on the submission. That theoretically hides it from 100% of spambot (I mean who would want to base64 decode every string checking if it's an e-mail or not :P), but to be technically 100% safe, maybe encrypt it with a secret key, (the one in settings.py for example), and decrypt it on submission.
Cheers for the nice piece
#
Thanks, Amr. Actually, the full e-mail address never shows up, even when viewing the source of the page. The links look something like http://www.domain.com/email/9/ and the only part of the e-mail that ever shows up is masked (like bra...@washburn.edu).
Here's an example using this: http://www.bluecamel.com/resume/
#