Django + Django-registration:如何使用HTML发送电子邮件

12

我正在使用django-registration,一切都很好,确认邮件已经以纯文本形式发送,但现在我更改后它以HTML形式发送,但是我有一个小问题... HTML代码正在显示:

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

我不想展示像这样的HTML代码...

有什么想法吗?

谢谢

4个回答

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

我该如何在注册后端中注册新的个人资料? - Sam
11
如何将后端设置为HtmlRegistration配置文件而不是RegistrationProfile? - AlexBrand
我们需要创建另一个注册后端来使用我们的新代理模型吗? - Pierre de LESPINAY
你如何设置新的默认值? - Azd325

15

我建议同时发送文本版本和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()

是的,保罗,谢谢你的回复,但它没有起作用,我按照那种方式做了,但什么也没有发生...但现在没问题了 :) ,现在只需放置链接而不需要<a ... - Asinox
这将发送一封文本电子邮件,其中一些客户将创建链接。如果您需要更有趣的HTML,您将不得不按照我推荐的方式去做。 - Paul Tarjan
是的,我尝试过了但不起作用,但没关系 :) 我会再试一下的。 - Asinox

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'),'

0

这个人扩展了默认后端,使我们能够添加激活电子邮件的HTML版本。

具体来说,备用版本的工作在这里完成。

我成功地使用了后端部分。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接