Django发送带有链接的电子邮件

mznpcxlj  于 2023-06-07  发布在  Go
关注(0)|答案(1)|浏览(395)

我有一个电子邮件验证,而注册,验证链接必须在一个超链接的形式。

from django.core.mail import send_mail
from django.conf import settings

def send_email_verification_mail(email, first_name, verify_link, exp):
    html = """
    <a href="{link}">Click to verify</a>
    """
    html.format(link=verify_link)
    subject = 'Your accounts need to be verified'
    message = f'Hi {first_name.capitalize()},\n\nClick on the link to verify your 
    account \n 
    \n{html}\n \nThis link will expire in {exp}'
    email_from = settings.EMAIL_HOST_USER
    recipient_list = [email]
    send_mail(subject, message, email_from, recipient_list)
    return True

This is the what I'm getting

yh2wf1be

yh2wf1be1#

format方法返回带格式的字符串,但不修改原始字符串。尝试将返回值赋给原始字符串。

html = html.format(link=verify_link)

文档说明send_mailhtml_message参数,
如果提供了html_message,则生成的电子邮件将是一个多部分/备选电子邮件,message作为text/plain内容类型,html_message作为text/html内容类型
因此,您可以按照this answer中所示的方法修改代码:

相关问题