我正在尝试设置一个HTML模板,该模板为主题标题和部分内容提供了一些字段形式。
views.py
if len(recipient) > 0:
messages.success(request, "Receipt sent successfully!")
subject = "Your Booking Reference: "
to = [recipient]
from_email = 'orders@demo.net'
template = get_template('booking/email/booking_reference.html')
message = EmailMessage(subject, template, from_email, ['test@test.com'])
message.content_subtype = 'html'
message.send()
return HttpResponse("Sent!")
else:
return index(request)
每当我请求发送电子邮件时,都会收到以下错误:
'Template' object has no attribute 'encode'
如果我注释掉message.content_subtype = 'html'
,我会收到所需的HttpResponse,但没有发送电子邮件。我已将此设置添加到我的settings.py
文件中,以便所有电子邮件都输出到控制台,但不显示任何内容
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
答案 0 :(得分:1)
尝试写下这个:
message = EmailMultiAlternatives (subject, template, from_email, [test@test.com])
message.attach_alternative(template, "text/html")
答案 1 :(得分:0)
好的我认为问题是你没有在模板中添加上下文所以试试这个:
if len(recipient) > 0:
messages.success(request, "Receipt sent successfully!")
subject = "Your Booking Reference: "
to = [recipient]
from_email = 'orders@demo.net'
template = loader.get_template('booking/email/booking_reference.html')
context = RequestContext(request, locals())
template = template.render(context)
message = EmailMessage(subject, template, from_email, ['test@test.com'])
message.content_subtype = 'html'
message.send(True)
return HttpResponse("Sent!")
else:
return index(request)