I have set up a fully operational contact form and now I would like to render the users inputs into a html template, so when I receive the email it is easier to read.
How do I do this? Do I link the template below some where, or link the form with in a template? Thank you in advance
def contactPage(request):
if request.method == 'POST':
form = contactForm(request.POST)
if form.is_valid():
subject = "INQUIRY"
body = {
'full_name': form.cleaned_data['full_name'],
'email': form.cleaned_data['email_address'],
'message':form.cleaned_data['message'],
}
message = "\n".join(body.values())
try:
send_mail(subject, message, '', ['email])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect ('thankyou')
form = contactForm()
return render(request, "contact.html", {'form':form})
One approach is to use the
html_messageparameter insend_mail().html_messagecan refer to an html template that will be used to present the message content. Here's what I have working:forms.py
views.py
email.html
email.txt
Sending a message through this form, I get one email in my inbox. Here's a screen shot:
A related SO question is: Creating email templates with Django
Note: My answer implements andilabs's answer from that post.
Docs reference: https://docs.djangoproject.com/en/4.1/topics/email/#send-mail