504网关超时发送邮件

时间:2016-07-17 16:40:00

标签: django apache nginx mod-wsgi

从Django的联系表单发送邮件时遇到问题。我在settings.py中设置了smtp配置,我可以从本地发送它。但在生产服务器中,我得到504网关超时错误。这是错误日志内容。

        [Sun Jul 17 18:17:46.142782 2016] [wsgi:error] [pid 16049] [client XXXXX:37298] Timeout when reading response headers from daemon process 'domain.gal': /home/wsgi/web/domain.gal/private/django/domain.gal/domain/wsgi.py, referer: http://domain.gal/contacto/
[Sun Jul 17 18:17:49.120971 2016] [wsgi:error] [pid 16046] Internal Server Error: /contacto/
[Sun Jul 17 18:17:49.121004 2016] [wsgi:error] [pid 16046] Traceback (most recent call last):
[Sun Jul 17 18:17:49.121008 2016] [wsgi:error] [pid 16046]   File "/home/wsgi/web/domain.gal/private/django/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
[Sun Jul 17 18:17:49.121012 2016] [wsgi:error] [pid 16046]     response = self.process_exception_by_middleware(e, request)
[Sun Jul 17 18:17:49.121015 2016] [wsgi:error] [pid 16046]   File "/home/wsgi/web/domain.gal/private/django/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
[Sun Jul 17 18:17:49.121018 2016] [wsgi:error] [pid 16046]     response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Sun Jul 17 18:17:49.121021 2016] [wsgi:error] [pid 16046]   File "/home/wsgi/web/domain.gal/private/django/domain.gal/home/views.py", line 43, in contacto
[Sun Jul 17 18:17:49.121023 2016] [wsgi:error] [pid 16046]     send_mail(subject, message, from_email, ['info@domain.es'])
[Sun Jul 17 18:17:49.121026 2016] [wsgi:error] [pid 16046]   File "/home/wsgi/web/domain.gal/private/django/env/lib/python3.4/site-packages/django/core/mail/__init__.py", line 61, in send_mail
[Sun Jul 17 18:17:49.121029 2016] [wsgi:error] [pid 16046]     return mail.send()
[Sun Jul 17 18:17:49.121032 2016] [wsgi:error] [pid 16046]   File "/home/wsgi/web/domain.gal/private/django/env/lib/python3.4/site-packages/django/core/mail/message.py", line 292, in send
[Sun Jul 17 18:17:49.121034 2016] [wsgi:error] [pid 16046]     return self.get_connection(fail_silently).send_messages([self])
[Sun Jul 17 18:17:49.121037 2016] [wsgi:error] [pid 16046]   File "/home/wsgi/web/domain.gal/private/django/env/lib/python3.4/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
[Sun Jul 17 18:17:49.121040 2016] [wsgi:error] [pid 16046]     new_conn_created = self.open()
[Sun Jul 17 18:17:49.121043 2016] [wsgi:error] [pid 16046]   File "/home/wsgi/web/domain.gal/private/django/env/lib/python3.4/site-packages/django/core/mail/backends/smtp.py", line 58, in open
[Sun Jul 17 18:17:49.121060 2016] [wsgi:error] [pid 16046]     self.connection = connection_class(self.host, self.port, **connection_params)
[Sun Jul 17 18:17:49.121063 2016] [wsgi:error] [pid 16046]   File "/usr/lib64/python3.4/smtplib.py", line 242, in __init__
[Sun Jul 17 18:17:49.121066 2016] [wsgi:error] [pid 16046]     (code, msg) = self.connect(host, port)
[Sun Jul 17 18:17:49.121069 2016] [wsgi:error] [pid 16046]   File "/usr/lib64/python3.4/smtplib.py", line 321, in connect
[Sun Jul 17 18:17:49.121071 2016] [wsgi:error] [pid 16046]     self.sock = self._get_socket(host, port, self.timeout)
[Sun Jul 17 18:17:49.121074 2016] [wsgi:error] [pid 16046]   File "/usr/lib64/python3.4/smtplib.py", line 292, in _get_socket
[Sun Jul 17 18:17:49.121076 2016] [wsgi:error] [pid 16046]     self.source_address)
[Sun Jul 17 18:17:49.121079 2016] [wsgi:error] [pid 16046]   File "/usr/lib64/python3.4/socket.py", line 512, in create_connection
[Sun Jul 17 18:17:49.121081 2016] [wsgi:error] [pid 16046]     raise err
[Sun Jul 17 18:17:49.121084 2016] [wsgi:error] [pid 16046]   File "/usr/lib64/python3.4/socket.py", line 503, in create_connection
[Sun Jul 17 18:17:49.121086 2016] [wsgi:error] [pid 16046]     sock.connect(sa)
[Sun Jul 17 18:17:49.121091 2016] [wsgi:error] [pid 16046] TimeoutError: [Errno 110] Connection timed out
[Sun Jul 17 18:17:49.121097 2016] [wsgi:error] [pid 16046]

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您的应用无法连接到指定的SMTP服务器。检查以下问题:

  • 您的SMTP详情是否正确?
  • 您在配置的主机/端口上有运行的SMTP服务器吗?
  • 连接是否被防火墙阻止?

从日志中可以看出,SMTP连接已超时。请提供更多详细信息(代码/配置)以便更好地推测。

答案 1 :(得分:0)

好的masnum是对的。我已经更改了端口和无法使用的TLS,它可以工作。但我有两个问题。我的配置如下:

EMAIL_PORT = 587
EMAIL_USE_TLS = True

这是对的吗?我可以在localhost中使用它,为什么不在生产服务器中?繁忙的港口可能吗?

另一个问题。我用这个:

try:
            send_mail(subject, message, from_email, ['info@domain.com'])
        except BadHeaderError:
            messages.warning(request, 'Ha ocurrido un fallo. Inténtelo de nuevo más tarde.')
            return render(request, "contacto.html", {'form': form})

        except TimeoutError:
            messages.warning(request, 'Ha ocurrido un fallo. Inténtelo de nuevo más tarde.')
            return render(request, "contacto.html", {'form': form})

为什么TimeoutError没有捕获错误而不是504 Apache?

TXH