I made a Modal contact form named "get in touch" (Bootstrap) in Django and its working 100% but I'm busy setting it up to send a mail towards my email address with the content of the contact form.
but I am getting an error saying
settings.EMAIL_HOST_USER
NameError: name 'settings' is not defined
I have added the mail settings in the settings.py file and set it in my views.py but I am getting this error and I don't know why?
views.py
from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from .forms import ContactForm
class MailSender():
host = settings.EMAIL_HOST_USER
def sendMail(self, formData, message, sender):
return send_mail(formData, message, self.host, sender, fail_silently = False)
class ContactMe(View):
template = "contact.html"
context = {
'form': ContactForm()
}
def get(self, request):
print("getting form")
return render(request, self.template, self.context)
def post(self, request):
form = ContactForm(request.POST)
if form.is_valid():
data = form.cleaned_data
print(data)
mailSender(ContactForm, data['message'], settings.EMAIL_HOST_USER, ['myemail@email.com'])
MailSender.sendMail()
if emailsent == True:
returnData = {
'success': True,
'message': 'Email not sent, but data received'
}
else:
returnData = {
'success': True,
'message': 'Email not sent, but data received'
}
# return JsonResponse(returnData, safe=False)
next = request.POST.get('next', '/')
return HttpResponseRedirect(next)
def post_regularDjango(self, request):
form = ContactForm(request.POST)
if form.is_valid():
data = form.cleaned_data
print(data['from_email'], data['subject'], data['subject'])
return render(request, self.template, self.context)
else:
print("form invalid")
self.context.form = form
return render(request, self.template, self.context)
Email settings in settings.py file
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@email.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Any help would be appreciated.
Kind Regards,