Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Update to django-email-confirmation #1

Open
wants to merge 1 commit into from

2 participants

Brad Beattie Patrick Altman
Brad Beattie

Adds HTML email support

Updates devproject to work with Django 1.3
Adds HTML email support
e1b6bc2
Patrick Altman
Owner

Brad,

Really appreciate your patch here and like the idea of email-confirmation supporting HTML emails, however, I would like to see it as an optional addition driven by a setting that defaults to False. So if something like EMAIL_CONFIRMATION_HTML_EMAILS = True is in settings.py then do the multipart building and rendering of the HTML template, otherwise, it functions as it always has.

Does that make sense?

Thanks,
Patrick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 16, 2011
Updates devproject to work with Django 1.3
Adds HTML email support
e1b6bc2
This page is out of date. Refresh to see the latest.
2  .gitignore
... ...
@@ -0,0 +1,2 @@
  1
+*.pyc
  2
+devproject/dev.db
9  devproject/devtest/forms.py
... ...
@@ -1,5 +1,5 @@
1  
-from django import newforms as forms
2  
-from django.core.validators import alnum_re
  1
+from django import forms
  2
+import re
3 3
 
4 4
 from django.contrib.auth.models import User
5 5
 from emailconfirmation.models import EmailAddress
@@ -14,7 +14,7 @@ class SignupForm(forms.Form):
14 14
     email = forms.EmailField(label="Email (optional)", required=False, widget=forms.TextInput())
15 15
     
16 16
     def clean_username(self):
17  
-        if not alnum_re.search(self.cleaned_data["username"]):
  17
+        if not re.compile(r'^\w+$').search(self.cleaned_data["username"]):
18 18
             raise forms.ValidationError(u"Usernames can only contain letters, numbers and underscores.")
19 19
         try:
20 20
             user = User.objects.get(username__exact=self.cleaned_data["username"])
@@ -35,7 +35,6 @@ def save(self):
35 35
         password = self.cleaned_data["password1"]
36 36
         new_user = User.objects.create_user(username, email, password)
37 37
         if email:
38  
-            self.user.message_set.create(message="Confirmation email sent to %s" % email)
39 38
             EmailAddress.objects.add_email(new_user, email)
40 39
         return username, password # required for authenticate()
41 40
 
@@ -58,4 +57,4 @@ def clean_email(self):
58 57
     def save(self):
59 58
         self.user.message_set.create(message="Confirmation email sent to %s" % self.cleaned_data["email"])
60 59
         return EmailAddress.objects.add_email(self.user, self.cleaned_data["email"])
61  
-        
  60
+        
1  devproject/emailconfirmation
2  devproject/urls.py
@@ -11,5 +11,5 @@
11 11
     
12 12
     (r'^confirm_email/(\w+)/$', 'emailconfirmation.views.confirm_email'),
13 13
     
14  
-    (r'^admin/(.*)', admin.site.root),
  14
+    (r'^admin/', include(admin.site.urls))
15 15
 )
10  emailconfirmation/models.py
@@ -3,7 +3,7 @@
3 3
 
4 4
 from django.conf import settings
5 5
 from django.db import models, IntegrityError
6  
-from django.core.mail import send_mail
  6
+from django.core.mail import send_mail, EmailMultiAlternatives
7 7
 from django.core.urlresolvers import reverse, NoReverseMatch
8 8
 from django.template.loader import render_to_string
9 9
 from django.utils.hashcompat import sha_constructor
@@ -118,9 +118,13 @@ def send_confirmation(self, email_address):
118 118
             "emailconfirmation/email_confirmation_subject.txt", context)
119 119
         # remove superfluous line breaks
120 120
         subject = "".join(subject.splitlines())
121  
-        message = render_to_string(
  121
+        text_content = render_to_string(
122 122
             "emailconfirmation/email_confirmation_message.txt", context)
123  
-        send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address.email])
  123
+        html_content = render_to_string(
  124
+            "emailconfirmation/email_confirmation_message.html", context)
  125
+        msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [email_address.email])
  126
+        msg.attach_alternative(html_content, "text/html")
  127
+        msg.send()
124 128
         confirmation = self.create(
125 129
             email_address=email_address,
126 130
             sent=datetime.datetime.now(),
10  emailconfirmation/templates/emailconfirmation/email_confirmation_message.html
... ...
@@ -0,0 +1,10 @@
  1
+<html>
  2
+<body>
  3
+<p>
  4
+{% load i18n %}{% blocktrans with current_site.name as site_name %}User '{{ user }}' at {{ site_name }} has given this as an email address.
  5
+
  6
+To confirm this is correct, go to {{ activate_url }}
  7
+{% endblocktrans %}
  8
+</p>
  9
+</body>
  10
+</html>
2  setup.py
@@ -12,7 +12,7 @@
12 12
     packages=find_packages(exclude=["devproject.devtest", "devproject"]),
13 13
     package_data = {
14 14
         "emailconfirmation": [
15  
-            "templates/emailconfirmation/*.txt"
  15
+            "templates/emailconfirmation/*"
16 16
         ],
17 17
     },
18 18
     zip_safe=False,
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.