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

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

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

任何想法?

谢谢

有帮助吗?

解决方案

我建议同时发送文本版本和HTML版本。看在django的登记的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()

其他提示

为了避免修补 django-registration,您应该使用以下命令扩展 RegistrationProfile 模型 代理=真:

模型.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()

在您的注册后端中,只需使用 Html注册资料 代替 注册资料.

我知道这是旧的和注册软件包不再保持。万一有人仍想这一点。 额外的步骤WRT到@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'),'

这家伙扩展了defaultBackend 使我们能够添加 HTML 版本的激活电子邮件。

具体来说,备用版本工作已完成 这里

我成功地使用了后端部分

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top