如何解决django邮件发送中的SMTPServerDisconnected?

时间:2017-05-10 12:12:44

标签: python django email smtp

当我在本地主机上测试它时,我如何解决这个问题,但它在服务器上不起作用。
这是我的电子邮件表格代码。

   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('/')

这是我的settings.py配置

    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

1 个答案:

答案 0 :(得分:0)

我认为您忘记为SMTP身份验证添加用户和密码,还是公共服务器?

在任何情况下,您都可以在调用电子邮件方法时使用 fail_silently 参数。这不会触发您获得的异常official docs

以下是一个例子:

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)

请注意 fail_silently = True

使用网络SMTP tester tool,我可以看到此服务器不支持未经身份验证的发送:

<<< 250 Reset OK 
>>> MAIL FROM: <test@127.0.0.1> 
<<< 250 OK 
>>> RCPT TO: <test@spam.com> 
<<< 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.