0

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,

Faziki
  • 767
  • 1
  • 9
  • 24
  • show used imports in views.py – iklinac Jan 12 '20 at 20:43
  • @iklinac Haha, sorry I forgot to add that. I have updated the post and you can see the imports in view.py section – Faziki Jan 12 '20 at 20:51
  • 3
    Does this answer your question? [How do I reference a Django settings variable in my models.py?](https://stackoverflow.com/questions/7867797/how-do-i-reference-a-django-settings-variable-in-my-models-py) – Swetank Poddar Jan 12 '20 at 21:02

1 Answers1

1

You haven't imported Django settings into views.py

from django.conf import settings
iklinac
  • 14,944
  • 4
  • 28
  • 30
  • Haha, now I feel silly! I think I accidently removed during testing and trying different methods. – Faziki Jan 12 '20 at 21:05