0

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)
dev dap
  • 85
  • 2
  • 8
  • possible duplicate question https://stackoverflow.com/questions/14949492/errno-101-network-is-unreachable-when-trying-to-send-email-using-django – Vincent Jan 14 '19 at 16:17
  • I would recommand checking this – StyleZ Jan 14 '19 at 16:17
  • Possible duplicate of [How to fix Network is unreachable with django's send\_mail?](https://stackoverflow.com/questions/32478431/how-to-fix-network-is-unreachable-with-djangos-send-mail) – StyleZ Jan 14 '19 at 16:18
  • That is related to my question..but it doesn't solve my problem...Only exception was gone but still email is not send ...I have put this line of code on my settings.py EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' – dev dap Jan 15 '19 at 03:39

1 Answers1

0

Most likely your provider is blocking the port used to send emails. It can also be caused by local network rules (check iptables rules if you're using linux and see if there're any outgoing traffic blocking ones).

pomo_mondreganto
  • 2,028
  • 2
  • 28
  • 56