python - 如何在 django email_user 中将 content_subtype 更改为 html

标签 python django email html-email

我从这个 tutorial 读到了关于 django 电子邮件确认的信息。现在我需要发送 html 邮件而不是简单的字符串。我读过这个answer关于如何在 django 中发送 html 电子邮件。有没有办法在本教程电子邮件发送方法中将 content_subtype 更改为 html?或者任何其他方式以这种方式发送 html 邮件?

current_site = get_current_site(request)
subject = 'Activate Your Account'
message = render_to_string('account_activation_email.html', {
    'user': user,
    'domain': current_site.domain,
    'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
    'token': account_activation_token.make_token(user),
    })
user.email_user(subject, message)

最佳答案

我尝试得到答案,希望它可以帮助其他人。

email_user函数是这样的:

def email_user(self, subject, message, from_email=None, **kwargs):
    """Send an email to this user."""
    send_mail(subject, message, from_email, [self.email], **kwargs)

这是send_mail函数:

def send_mail(subject, message, from_email, recipient_list,
              fail_silently=False, auth_user=None, auth_password=None,
              connection=None, html_message=None):
    """
    Easy wrapper for sending a single message to a recipient list. All members
    of the recipient list will see the other recipients in the 'To' field.

    If auth_user is None, use the EMAIL_HOST_USER setting.
    If auth_password is None, use the EMAIL_HOST_PASSWORD setting.

    Note: The API for this method is frozen. New code wanting to extend the
    functionality should use the EmailMessage class directly.
    """
    connection = connection or get_connection(
       username=auth_user,
       password=auth_password,
       fail_silently=fail_silently,
    )
    mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)
    if html_message:
        mail.attach_alternative(html_message, 'text/html')

    return mail.send()

首先有一个 html_message 属性,我认为它可以像电子邮件附件一样处理,但我测试了它并且它有效。

这是我发送 html 电子邮件的代码:

        current_site = get_current_site(request)
        subject = 'Activate Your Account'
        message = render_to_string('account_activation_email.html', {
            'user': user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
            'token': account_activation_token.make_token(user),
        })
        user.email_user(subject, '', html_message=message)

来自django docs :

html_message: If html_message is provided, the resulting email will be a multipart/alternative email with message as the text/plain content type and html_message as the text/html content type

关于python - 如何在 django email_user 中将 content_subtype 更改为 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52244151/

相关文章:

Python PyQt如何为QProcess设置环境变量?

python - 是否可以在 Vista 上构建 exe 并使用 py2exe 在 XP 上部署

javascript - swampdragon如何导入js文件?

c# - 单击过期链接如何在重置密码的上下文中工作

php - 如何在 php 中实现电子邮件发布系统

HTML 电子邮件 - 将图像置于已定义宽度和高度的元素内

python - python2.5中的Exception_Record问题

Python:修改.cfg文件中的部分字符串

python - 具有多个数据库的 Django,来自管理员中非默认数据库权限的模型

python - 在django.shortcuts.render和django.views.generic.TemplateView之间选择