django+ send email in html with django-registration

后端 未结 4 1570
南笙
南笙 2020-12-07 23:52

im using django-registration, all is fine, the confirmation email was sending in plain text, but know im fixed and is sending in html, but i have a litter problem... the htm

相关标签:
4条回答
  • 2020-12-08 00:05

    To avoid patching django-registration, you should extend the RegistrationProfile model with proxy=True:

    models.py

    class HtmlRegistrationProfile(RegistrationProfile):
        class Meta:
            proxy = True
        def send_activation_email(self, site):
            """Send the activation mail"""
            from django.core.mail import EmailMultiAlternatives
            from django.template.loader import render_to_string
    
            ctx_dict = {'activation_key': self.activation_key,
                        'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                        'site': site}
            subject = render_to_string('registration/activation_email_subject.txt',
                                       ctx_dict)
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())
    
            message_text = render_to_string('registration/activation_email.txt', ctx_dict)
            message_html = render_to_string('registration/activation_email.html', ctx_dict)
    
            msg = EmailMultiAlternatives(subject, message_text, settings.DEFAULT_FROM_EMAIL, [self.user.email])
            msg.attach_alternative(message_html, "text/html")
            msg.send()
    

    And in your registration backend, just use HtmlRegistrationProfile instead of RegistrationProfile.

    0 讨论(0)
  • 2020-12-08 00:11

    This guy have extended the defaultBackend enabling us to add an HTML version of the activation email.

    Specifically, the alternate version job is done here

    I managed to use the backend part successfully

    0 讨论(0)
  • 2020-12-08 00:14

    I know this is old and the registration package is no longer maintained. Just in case somebody still wants this. The additional steps wrt to the answer of @bpierre are:
    - subclass the RegistrationView, i.e. your app's views.py

    class MyRegistrationView(RegistrationView):
    ...
    def register(self, request, **cleaned_data):
        ...
        new_user = HtmlRegistrationProfile.objects.create_inactive_user(username, email, password, site)
    

    - in your urls.py change the view to the sub-classed view, i.e. - List item

    url(r'accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'
    
    0 讨论(0)
  • 2020-12-08 00:22

    I'd recommend sending both a text version and an html version. Look in the models.py of the django-registration for :

    send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
    

    and instead do something like from the docs http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types

    from django.core.mail import EmailMultiAlternatives
    
    subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
    text_content = 'This is an important message.'
    html_content = '<p>This is an <strong>important</strong> message.</p>'
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()
    
    0 讨论(0)
提交回复
热议问题