仅获取 SMTPServerDisconnected:请在调用服务器时先运行 connect()

问题描述

我最近使用 Sendgrid smtp 作为后端为 Django 启用了电子邮件发送功能

这是我的电子邮件设置:

#AUTO SEND EMAIL
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = 'the_sendgrid_key'
EMAIL_PORT = 587

在我的其中一个 api 中得到以下代码

student = request.user
subject = 'test',text_content = 'test'
email = EmailMultiAlternatives(subject,text_content,os.environ.get('DEFAULT_FROM_EMAIL'),to=[student.email])
email.attach_alternative(html_content,"text/html")
email.send()

在本地开发期间,每当我调用 api 时,都会发送电子邮件并正常接收

但是当我在服务器上调用 api 时,它不断返回以下错误

SMTPServerdisconnected: please run connect() first (Most recent call last)
File /root/study/api/views/views.py line 1470 in post args locals
email.send()
File /usr/lib/python3.8/smtplib.py line 753 in starttls args locals
self.ehlo_or_helo_if_needed()
File /usr/lib/python3.8/smtplib.py line 604 in ehlo_or_helo_if_needed args locals
if not (200 <= self.ehlo()[0] <= 299):
File /usr/lib/python3.8/smtplib.py line 444 in ehlo args locals
self.putcmd(self.ehlo_msg,name or self.local_hostname)
File /usr/lib/python3.8/smtplib.py line 371 in putcmd args locals
self.send(str)
File /usr/lib/python3.8/smtplib.py line 363 in send args locals
raise SMTPServerdisconnected('please run connect() first')
SMTPServerdisconnected: please run connect() first

查找此错误后,只回答了不正确的电子邮件设置,但我检查了电子邮件设置是完全正确的(因为我在本地收到了电子邮件

另一件奇怪的事情是,当我在服务器上运行 python manage.py shell 并直接调用 email.send() 方法时,没有弹出错误并收到电子邮件

我一周以来一直在寻找解决方案,但似乎无法找到为什么只有在调用包含该方法的 api 时才会弹出错误,希望有类似问题的人可以帮助我解决

更新: 我尝试按照评论中的建议在视图中手动打开和关闭

from django.core.mail import EmailMultiAlternatives,get_connection,EmailMessage

connection = get_connection()

# Manually open the connection
connection.open()

# Construct an email message that uses the connection
email1 = EmailMessage(
    subject,to=[student.email],connection=connection,)
email1.send() # Send the email
connection.close()

但仍然收到相同的 connect() 错误

SMTPServerdisconnected: please run connect() first (Most recent call last)
File /root/study/api/views/views.py line 1474 in post args locals
connection.open()
Show 1 non-project frame
File /usr/lib/python3.8/smtplib.py line 753 in starttls args locals
self.ehlo_or_helo_if_needed()
File /usr/lib/python3.8/smtplib.py line 604 in ehlo_or_helo_if_needed args locals
if not (200 <= self.ehlo()[0] <= 299):
File /usr/lib/python3.8/smtplib.py line 444 in ehlo args locals
self.putcmd(self.ehlo_msg,name or self.local_hostname)
File /usr/lib/python3.8/smtplib.py line 371 in putcmd args locals
self.send(str)
File /usr/lib/python3.8/smtplib.py line 363 in send args locals
raise SMTPServerdisconnected('please run connect() first')
SMTPServerdisconnected: please run connect() first

更新 2:

我的服务器启用了 SSL,在带有 Nginx 的 gunicorn 上运行以提供静态文件重定向端口

gunicorn 配置:

[Unit]
Description=Gunicorn daemon for Django Project
Before=Nginx.service
After=network.target

[Service]
WorkingDirectory=/root/study
ExecStart=/root/.cache/pypoetry/virtualenvs/base.django-H96T9Ltg-py3.8/bin/gunicorn --log-level=debug  --access-logfile /var/log/gunicorn/access.log --error-logfile /var/log/gunicorn/error.log --workers 5 --bind unix:/var/log/gunicorn/hoola.sock base.wsgi:application
Restart=always
SyslogIdentifier=gunicorn
User=root
Group=www-data


[Install]
WantedBy=multi-user.target

Nginx 配置:

user root;
worker_processes auto;
pid /run/Nginx.pid;
include /etc/Nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/Nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3,ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/Nginx/access.log;
    error_log /var/log/Nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+RSS text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/Nginx/conf.d/*.conf;
    include /etc/Nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.Nginx.org/ImapAuthenticateWithApachePHPScript
#
#   # auth_http localhost/auth.PHP;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIdplUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

更新 5: 来自 rollbar 错误日志的更多详细错误回溯:

SMTPServerdisconnected: please run connect() first (Most recent call last)
File /root/study/api/views/views.py line 1729 in get args locals
email.send()
获取参数
“自我” ""
“请求” ""
获取局部变量
e ""
电子邮件 ""
请求 ""
自己 ""
Hide 3 non-project frames
File /root/.cache/pypoetry/virtualenvs/base.django-H96T9Ltg-py3.8/lib/python3.8/site-packages/django/core/mail/message.py line 284 in send args locals
return self.get_connection(fail_silently).send_messages([self])
发送参数
“自我” "
"fail_silently" 未知
发送局部变量
fail_silently
自己 ""
File /root/.cache/pypoetry/virtualenvs/base.django-H96T9Ltg-py3.8/lib/python3.8/site-packages/django/core/mail/backends/smtp.py line 102 in send_messages args locals
new_conn_created = self.open()
send_messages 参数
“自我” ""
"email_messages ["]
send_messages 局部变量
email_messages [""]
自己 ""
File /root/.cache/pypoetry/virtualenvs/base.django-H96T9Ltg-py3.8/lib/python3.8/site-packages/django/core/mail/backends/smtp.py line 67 in open args locals
self.connection.starttls(keyfile=self.ssl_keyfile,certfile=self.ssl_certfile)
公开辩论
“自我” ""
打开局部变量
connection_params {"local_hostname": "mydomain.io"}
自己 ""
File /usr/lib/python3.8/smtplib.py line 753 in starttls args locals
starttls 参数
“自我” ""
“密钥文件 未知
“证书文件 未知
“上下文” 未知
starttls 局部变量
证书文件
上下文
密钥文件
自己 ""
File /usr/lib/python3.8/smtplib.py line 604 in ehlo_or_helo_if_needed args locals
if not (200 <= self.ehlo()[0] <= 299):
ehlo_or_helo_if_needed 参数
“自我” ""
ehlo_or_helo_if_needed 参数
自己 ""
File /usr/lib/python3.8/smtplib.py line 444 in ehlo args locals
self.putcmd(self.ehlo_msg,name or self.local_hostname)
ehlo 参数
“自我” ""
“姓名” 未知
ehlo 局部变量
名称 ""
自己 ""
File /usr/lib/python3.8/smtplib.py line 371 in putcmd args locals
self.send(str)
putcmd 参数
“自我” ""
“命令” "ehlo"
"参数" "mydomain.io"
putcmd 局部变量
参数 "mydomain.io"
命令 "ehlo"
自己 ""
str "ehlo mydomain.io\r\n"
File /usr/lib/python3.8/smtplib.py line 363 in send args locals
raise SMTPServerdisconnected('please run connect() first')
发送参数
“自我” ""
"s" "ehlo mydomain.io\r\n"
发送局部变量
s "ehlo mydomain.io\r\n"
自己 ""
SMTPServerdisconnected: please run connect() first

解决方法

所以我终于找到了为什么它似乎永远无法连接,这是因为我为 gunicorn 设置的整个项目缺少 .env 文件变量。这就是为什么 EMAIL_HOST 和 EMAIL 配置在视图中运行时从不加载的原因。

我将 loadenv 添加到我的 wsgi.py 文件中,以便 gunicorn 可以加载它:

import os

from django.core.wsgi import get_wsgi_application
from dotenv import load_dotenv

load_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)),'.env'))
#load env before running wsgi
os.environ.setdefault("DJANGO_SETTINGS_MODULE","base.settings")
 
application = get_wsgi_application()

也改变了我的gunicorn.service运行命令的方式;

在 ExecStart 中,我从 base.wsgi:application 更改为 base.wsgi,因此它还加载了整个 dotenv 变量,而不仅仅是应用程序