I'm trying to setup a HTML template that takes in some field forms for the subject header, and for part of the content.
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)
Whenever I request an email to be sent, I get the following error: 'Template' object has no attribute 'encode'
If I comment out message.content_subtype = 'html'
, I get the desired HttpResponse, but with no e-mail sent. I've added this setting to my settings.py
file so that all e-mails get output to the console, but nothing is displayed
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Try to write this :
message = EmailMultiAlternatives (subject, template, from_email, [test@test.com])
message.attach_alternative(template, "text/html")
Ok I think the problem is that you don't add a context in your template so try this:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.