Django sendmail SSL错误EOF发生违反协议

时间:2017-08-07 20:46:00

标签: python django email ssl

我有这个令人讨厌的错误,我无法解决这个问题。一直试图解决它已经有一段时间了。任何帮助都会受到高度关注。我将粘贴堆栈跟踪,我还使用Wireshark进行了一些调试,我也会发布它。

我尝试升级可能与ssl通信相关的python包而没有任何成功。只有当EMAIL_USE_TLS为True时才会出现此问题,并且调试模式和生产都会发生此问题。我首先想到的可能是SMTP服务器在旧版本的ssl上使用,但事实并非如此。由于Wireshark捕获将告诉ssl似乎已建立,然后在下一个请求中服务器将意外关闭。

我使用的设置是:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp_server'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'my_username'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_USE_TLS = True

发送邮件时我没什么奇怪的,这里是代码:

def email_user(self, subject, message, from_email=None, **kwargs):
    return send_mail(subject, message, from_email, [self.email], **kwargs)

错误堆栈跟踪是:

Traceback (most recent call last):
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\rest_framework\views.py", line 477, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\rest_framework\views.py", line 437, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\rest_framework\views.py", line 474, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\rest_framework\decorators.py", line 52, in handler
    return func(*args, **kwargs)
  File "C:\Users\Metadeath\Documents\GitHub\django-dropin\api\views.py", line 18, in register
    user = serializer.create(serializer.validated_data)
  File "C:\Users\Metadeath\Documents\GitHub\django-dropin\api\serializers.py", line 86, in create
    is_sent = user.send_confirmation_email(confirmation_instance=confirmation_instance)
  File "C:\Users\Metadeath\Documents\GitHub\django-dropin\users\models.py", line 139, in send_confirmation_email
    is_sent = self.email_user(subject, message, from_email)  # Returns the number of delivered emails
  File "C:\Users\Metadeath\Documents\GitHub\django-dropin\users\models.py", line 132, in email_user
    return send_mail(subject, message, from_email, [self.email], **kwargs)
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\core\mail\__init__.py", line 62, in send_mail
    return mail.send()
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\core\mail\message.py", line 342, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\core\mail\backends\smtp.py", line 100, in send_messages
    new_conn_created = self.open()
  File "C:\Users\Metadeath\Envs\dropin\lib\site-packages\django\core\mail\backends\smtp.py", line 64, in open
    self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
  File "c:\users\metadeath\appdata\local\programs\python\python36-32\Lib\smtplib.py", line 770, in starttls
    server_hostname=self._host)
  File "c:\users\metadeath\appdata\local\programs\python\python36-32\Lib\ssl.py", line 401, in wrap_socket
    _context=self, _session=session)
  File "c:\users\metadeath\appdata\local\programs\python\python36-32\Lib\ssl.py", line 808, in __init__
    self.do_handshake()
  File "c:\users\metadeath\appdata\local\programs\python\python36-32\Lib\ssl.py", line 1061, in do_handshake
    self._sslobj.do_handshake()
  File "c:\users\metadeath\appdata\local\programs\python\python36-32\Lib\ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:749)

Wireshark capture for client and smtp server when establishing ssl connection

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

证书是一个问题,托管SMTP服务器的公司他们遇到了一些Python和一些PHP模块的问题。我怀疑它必须是我的openSSL版本(OpenSSL 1.0.2j)。现在它已经解决了,所以这个线程可能会被关闭。

如果有其他人遇到此问题,请与您的SMTP主机公司联系,他们可能会为您提供不同的中继。

祝所有开发者好运!