django用django-registration用html发送电子邮件

时间:2009-08-25 04:07:56

标签: django email django-templates django-registration

即时通讯使用django-registration,一切都很好,确认电子邮件是以纯文本形式发送的,但我知道我已修复并正在发送html,但我有一个垃圾问题... html代码显示:

<a href="http://www.example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/">http://www. example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/</a>

我不需要像......那样显示html代码。

有什么想法吗?

由于

4 个答案:

答案 0 :(得分:27)

为避免修补django-registration,您应该使用proxy=True扩展RegistrationProfile模型:

<强> 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()

在您的注册后端,只需使用 HtmlRegistrationProfile 而不是 RegistrationProfile

答案 1 :(得分:14)

我建议同时发送文本版和html版。查看django-registration的models.py:

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

而是从文档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()

答案 2 :(得分:2)

我知道这已经过时了,不再维护注册包。以防有人仍然想要这个。 @bpierre答案的附加步骤是:
  - 将RegistrationView子类化,即你的应用程序的views.py

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

- 在你的urls.py中将视图更改为子类视图,即   - 列出项目

url(r'accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'

答案 3 :(得分:0)

This guy have extended the defaultBackend使我们能够添加激活电子邮件的HTML版本。

具体而言,备用版本作业已完成here

我设法成功使用了后端部分

相关问题