Hi there i have contact form where user put the email address and an email will be sent locally. But when i use this code in production it will give the exception in Django like this.
Exception Type: OSError at /contact-us/
Exception Value: [Errno 101] Network is unreachable
In settings.py
i have use this code for sending emails
DEBUG = True
ALLOWED_HOSTS = ['*']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_email_address'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
code of forms.py
class ContactForm(forms.ModelForm):
class Meta:
model = Consulting
#fields = ['email']
fields = '__all__'
widgets = {
'email': forms.TextInput(attrs={'class': 'form-control form-control-lg'}),
'name': forms.TextInput(attrs={'class': 'form-control form-control-lg'}),
'content': forms.TextInput(attrs={'class': 'form-control form-control-lg textarea-autosize'}),
}
def clean_email(self):
email = self.cleaned_data.get('email')
return email
code written in views.py
def contact_page(request):
contactform = ContactForm(request.POST or None)
if contactform.is_valid():
instance = contactform.save(commit=False)
if Consulting.objects.filter(email=instance.email).exists():
messages.warning(request, 'Your Email Is Already Exists', "alert alert-warning alert-dismissible")
print("This Email Is Already Registered")
else:
instance.save()
messages.success(request, 'Thanks For The contact.', "alert alert-success alert-dismissible")
subject = "Thankyou for joining our newsletter"
from_email = settings.EMAIL_HOST_USER
to_email = [instance.email]
signup_message = """ welcome to devdap contact """
send_mail(subject=subject, from_email=from_email, recipient_list=to_email, message=signup_message, fail_silently=False)
context = {
'form': contactform
}
return render(request, "pages/contact_us.html", context)