Fady khallaf
Fady khallaf

Reputation: 23

how to solve SMTPServerDisconnected in django email sending?

How I can solve this issue when I test this on my localhost it's work but it doesn't work in server.
this is my code for email form.

   class ContactCleaningView(TemplateView):
   template_name = 'contact_cleaning.html'

   def get(self, request, *args, **kwargs):
          context = self.get_context_data(**kwargs)
          context['form'] = ContactCleaning()
          return self.render_to_response(context)

   def post(self, request, *args, **kwargs):
          context = self.get_context_data(**kwargs)
          form = ContactCleaning(request.POST)

          if form.is_valid():
                 name = request.POST['name']
                 phone = request.POST['phone']
                 address =  request.POST['address']
                 email = request.POST['email']
                 zipcode = request.POST['zipcode']
                 rooms = request.POST['rooms']
                 bathrooms = request.POST['bathrooms']
                 service = request.POST['service']
                 subject = 'Cleaning-' + request.POST['subject']
                 description =  request.POST['description']
                 send_mail('From sams cleaning and hauling', "Name:"+name+"\nPhone:"+phone+"\nAddress:"+address+"\nZipcode:"+zipcode+"\nNumber Of Rooms:"+rooms+"\nNumber Of Bathrooms:"+bathrooms+"\nType Of Service:"+service+"\nSubject:"+subject+"\nDescription:"+description, email, [
                                 settings.DEFAULT_FROM_EMAIL, ], fail_silently=False)
                 # contact_form = ContactCleaning(name=name, email= email, phone=phone,address=address, subject=subject, message=message)
                 # contact_form.save()
          # context['form'] = form
          # return self.render_to_response(context)
          return HttpResponseRedirect('/')

and this is my settings.py configuration

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_USE_TLS = False
    EMAIL_HOST = 'mail.cleaningandhauling.us'
    EMAIL_PORT = 25
    EMAIL_HOST_USER = ''
    EMAIL_HOST_PASSWORD = ''
    DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

Upvotes: 1

Views: 2400

Answers (1)

I think you forgot to add user and password for SMTP authentication, or is it a public server?

In any case, you could use fail_silently paramater, when invoking email method. This will not trigger the exception that you are getting, official docs.

Here is an example:

send_mail('From sams cleaning and hauling', "Name:"+name+"\nPhone:"+phone+"\nAddress:"+address+"\nZipcode:"+zipcode+"\nNumber Of Rooms:"+rooms+"\nNumber Of Bathrooms:"+bathrooms+"\nType Of Service:"+service+"\nSubject:"+subject+"\nDescription:"+description, email, [
settings.DEFAULT_FROM_EMAIL, ], fail_silently=True)

Notice the fail_silently=True.

Using a web SMTP tester tool, I can see that this server doesn't support unauthenticated sends:

<<< 250 Reset OK 
>>> MAIL FROM: <[email protected]> 
<<< 250 OK 
>>> RCPT TO: <[email protected]> 
<<< 550-Please turn on SMTP Authentication in your mail client, or login to the
<<< 550-IMAP/POP3 server before sending your message. ns3000624.ovh.net 
<<< 550-(www.test-smtp.com) [37.59.46.82]:53257 is not permitted to relay through 
<<< 550 this server without authentication. 

Upvotes: 1

Related Questions