我是Django的新手,我使用Django 4.2。我试图从views.py以渲染模板的形式发送电子邮件。电子邮件发送的是纯文本。链接在电子邮件中不显示为链接,它显示为纯html文本。
views.py发送邮件功能
def sendActivationMail(user, request):
current_site = get_current_site(request)
email_subject = 'Verify your account'
# context = {
# 'click_action': 'showDomainLink',
# }
email_body = render_to_string(
'mail_temp/confirm_temp.html',
{
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': generator_token.make_token(user),
},
# context
)
email = EmailMessage(
subject=email_subject,
body=email_body,
from_email=settings.EMAIL_HOST_USER,
to=[user.email]
)
email.content_subtype = 'html'
email.send()
def activate_token(request, uidb64, token):
try:
uid = force_str(urlsafe_base64_decode(uidb64))
user = Users.objects.get(pk=uid)
except (ValueError, Users.DoesNotExist):
user = None
if user is not None and generator_token.check_token(user, token):
user.is_email_verified = True
user.save()
messages.success(request, 'Successfully verified email')
return redirect(reverse('login'))
else:
messages.error(request, 'Verification link was invalid')
return redirect('index')
邮件正文模板
{% autoescape off %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
h1{
color: chocolate;
}
p{
font-size: 22px;
}
</style>
</head>
<body>
<h1>Hey there, you are almost there!</h1>
<p>Please click link to verify your account</p>
<a class="underline" href="http://{{ domain }} {% url 'activate' uidb64=uid token=token%}">token</a>
</body>
</html>
{% endautoescape %}
我已经尝试谷歌这个问题,但我还没有找到一个匹配的解决方案.电子邮件一直发送为纯字符串.我必须做什么,让我的锚标签是可见的,作为一个链接在电子邮件正文.
1条答案
按热度按时间w1jd8yoj1#
我有一个更新。这是一个愚蠢的事情真的,但空间之间的域名和网址在模板页面不应该有。
它应该从这个开始:
对此: