通过 django 发送电子邮件不适用于 Gunicorn / NGinx`

man*_*pt1 -2 email email-server nginx django gunicorn

我正在尝试从我的 django 应用程序发送电子邮件。

但是,这不适用于我的 NGINX(用于静态资源)+ GUNICORN 服务器。

但是,同样适用于 django 的默认网络服务器。到目前为止,我还没有设置邮件服务器本身。

我不确定问题是什么。

让我解释整个情况,因为这个问题没有得到很好的解释。

如果我使用默认调试 WSGI 从 Django 中的 Web 应用程序发送电子邮件,

和以下代码

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)
Run Code Online (Sandbox Code Playgroud)

我可以发送电子邮件。

但是,我已经设置了一个 nginx 服务器和一个 gunicorn wsgi。我无法发送邮件。

我的 nginx.conf 看起来像这样

server {
        listen 80;
        listen 443;
        listen 587;
        server_name 172.17.110.205;
        client_max_body_size 0M;

        access_log /home/msrb_db/msrb_db_application/access.log;
        error_log /home/msrb_db/msrb_db_application/error.log;

        location /static {
                root /home/msrb_db/msrb_db_application;
        }

        location / {
                proxy_pass http://172.17.110.205:8080;
        }
}
Run Code Online (Sandbox Code Playgroud)

我有一个 gunicorn 服务器作为系统文件,其工作方式如下

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=msrb_db
Group=users
WorkingDirectory=/home/msrb_db/msrb_db_application/
ExecStart=/usr/bin/gunicorn --bind 172.17.110.205:8080 --workers 3 --pythonpath /home/msrb_db/msrb_db_application/ msrb_db_application.wsgi:application

[Install]
WantedBy=multi-user.target
~
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用其他 api for gmail 使用 EmailMessageClass

我的settings.py如下

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 443
EMAIL_HOST_USER = 'abc@abc.com
EMAIL_HOST_PASSWORD = 'plplplp'
~
Run Code Online (Sandbox Code Playgroud)

我不确定我在这里错过了什么。任何帮助表示赞赏。

Django 日志:

嗨,所以我从 Django 中取出了日志,没有发现与电子邮件相关的异常

/post/test_csv.zip' already exists
Nov 19 10:06:10 msrbdb gunicorn[58290]: sending email
Nov 19 10:06:10 msrbdb gunicorn[58290]: email sent
Nov 19 10:06:10 msrbdb gunicorn[58290]: Exception SystemExit: 0 in <module 'threading' from '/usr/lib64/python2.7/threading.pyc'> ignored
Run Code Online (Sandbox Code Playgroud)

而且,NGINX 没有错误

非常感谢。

Dia*_*ond 5

EMAIL_PORT = 443您用来连接 smtp 主机的端口号 ( ) 是错误的。它永远不会奏效。TCP 端口 443 是为 HTTPS 保留的。请尝试使用 google 规定的端口 465(需要 SSL)或端口 587(需要 TLS)。

您还在listen 587;nginx.conf 中使用了标准的 SMTP 端口 587 。你应该删除它。

有关详细信息,请参阅此处的 google 参考:https : //support.google.com/a/answer/176600?hl=en