1. bes
  2. django-registration

Commits

bes  committed 182c3dc Draft

send multipart messages if html template exists

  • Participants
  • Parent commits f95f29d
  • Branches default

Comments (0)

Files changed (1)

File registration/models.py

View file
 
 from django.conf import settings
 from django.contrib.auth.models import User
+from django.core.mail import EmailMultiAlternatives
 from django.db import models
 from django.db import transaction
 from django.template.loader import render_to_string
+from django.template import TemplateDoesNotExist
 from django.utils.hashcompat import sha_constructor
 from django.utils.translation import ugettext_lazy as _
 
         subject = render_to_string('registration/activation_email_subject.txt',
                                    ctx_dict)
         # Email subject *must not* contain newlines
-        subject = ''.join(subject.splitlines())
+        subject = ' '.join(subject.splitlines())
         
-        message = render_to_string('registration/activation_email.txt',
-                                   ctx_dict)
-        
-        self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
-    
+        message = render_to_string('registration/activation_email.txt', ctx_dict)
+        try:
+            message_html = render_to_string('registration/activation_email.html', ctx_dict)
+        except TemplateDoesNotExist:
+            message_html = None
+
+        email_message = EmailMultiAlternatives(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
+        if message_html:
+            email_message.attach_alternative(message_html, 'text/html')
+
+        email_message.send()
+