Django:发送电子邮件导致OSError

时间:2019-03-28 08:55:19

标签: django docker django-rest-framework

我正在为Django REST API设置注册过程。我在SignUpView中使用“ email.send()”功能。当我创建新用户时,它应该向他发送激活链接。不幸的是,我收到“ OSError:[Errno 99]无法分配请求的地址”。

我一直在寻找一些解决方案,但是所有解决方案都仅限于套接字的一些问题。尽管如此,我还是尝试在设置中更改端口。

settings.py中的我的电子邮件设置:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD= 'mypassword'
EMAIL_PORT = 587

我曾经注册过的视图:

class SignUp(APIView):

    def post(self, request, format=None):
        serializer = SignUpSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save(is_active=False)
            current_site = get_current_site(request)
            mail_subject = 'Aktywuj swoje konto w serwisie Podwoozka.'
            message =  'user: ', user.username,'domain', current_site.domain,'uid',urlsafe_base64_encode(force_bytes(user.pk)).decode(),'token', account_activation_token.make_token(user), 
            to_email = user.email
            email = EmailMessage(
                mail_subject, message, to=[to_email]
            )
            email.send()
            return HttpResponse('Potwierdz email aby zakonczyc rejestracje.')
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

最后,当我使用此功能时,整个回溯:


Traceback (most recent call last):
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
api_1  |     response = get_response(request)
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
api_1  |     response = self.process_exception_by_middleware(e, request)
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
api_1  |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
api_1  |     return view_func(*args, **kwargs)
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
api_1  |     return self.dispatch(request, *args, **kwargs)
api_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 495, in dispatch
api_1  |     response = self.handle_exception(exc)
api_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 455, in handle_exception
api_1  |     self.raise_uncaught_exception(exc)
api_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 492, in dispatch
api_1  |     response = handler(request, *args, **kwargs)
api_1  |   File "/Dback/races/views.py", line 264, in post
api_1  |     email.send()
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
api_1  |     return self.get_connection(fail_silently).send_messages([self])
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
api_1  |     new_conn_created = self.open()
api_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
api_1  |     self.connection = self.connection_class(self.host, self.port, **connection_params)
api_1  |   File "/usr/local/lib/python3.7/smtplib.py", line 251, in __init__
api_1  |     (code, msg) = self.connect(host, port)
api_1  |   File "/usr/local/lib/python3.7/smtplib.py", line 336, in connect
api_1  |     self.sock = self._get_socket(host, port, self.timeout)
api_1  |   File "/usr/local/lib/python3.7/smtplib.py", line 307, in _get_socket
api_1  |     self.source_address)
api_1  |   File "/usr/local/lib/python3.7/socket.py", line 727, in create_connection
api_1  |     raise err
api_1  |   File "/usr/local/lib/python3.7/socket.py", line 716, in create_connection
api_1  |     sock.connect(sa)
api_1  | OSError: [Errno 99] Cannot assign requested address

我不知道它是否重要,但是我在这个项目中使用了docker。我听说有时容器的传出连接存在一些问题。

编辑:我试图在pipenv而不是docker中运行它,仍然无法正常工作。 这次是“ ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它”

1 个答案:

答案 0 :(得分:2)

首先,请确保您已在Gmail中启用POP访问:

https://support.google.com/mail/answer/7104828?hl=en&visit_id=637244831918669326-2427073497&rd=1

然后,如果您收到Application-specific password required错误,请为您的帐户设置应用密码:

https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor&visit_id=637244831918669326-2427073497&rd=1