I created a Contact form on my website built in Django but emails sent from the form do not seem to actually send. Here is my form code in forms.py:
class ContactForm(forms.Form):
email = forms.EmailField(required=True)
subject = forms.CharField(required=True)
message = forms.CharField(widget=forms.Textarea, required=True)
Here is the code in my views.py:
def contact(request, success=""):
submitted = False
template_name = "main/contact.html"
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
from_email = form.cleaned_data['email']
message = form.cleaned_data['message']
try:
send_mail(subject, message, from_email, ['MYEMAILHERE'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect("success/")
if success != "":
submitted = True
context = {"form": form,
"submitted": submitted}
return render(request, template_name, context)
And finally here is the forms html code:
{% load crispy_forms_tags %}
<h1>Contact Us</h1>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<div class="form-actions">
<button type="submit" class="btn btn-primary mb-2">Send</button>
</div>
</form>
If any other code is needed to help debug please let me know and I will include it. Thank you.