【发布时间】:2021-02-07 02:29:45
【问题描述】:
我正在使用 Mac Catalina 10.15.7 / django 3.1.2 / python 3.8.5。
我正在尝试从 django 发送电子邮件。
首先,我在settings.py中定义了这些
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS=True
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=587
EMAIL_HOST_USER='MYID@gmail.com'
EMAIL_HOST_PASSWORD='MYPASSWORD'
SERVER_EMAIL='MYID@gmail.com'
DEFAULT_FROM_EMAIL=EMAIL_HOST_USER
然后,我进入 gmail 设置并 1) 启用 IMAP 使用和 2)(在帐户的安全设置中)启用低安全应用程序的使用 (https://www.google.com/settings/security/lesssecureapps)
那么,在我的 pycharms 的 python 控制台中,
from django.core.mail import EmailMessage
email = EmailMessage('subject text', 'body text', to=['RECEIVINGEMAILID@gmail.com'])
但我收到以下错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/capstone1/lib/python3.8/site-packages/django/core/mail/message.py", line 225, in __init__
self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
File "/Users/capstone1/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
self._setup(name)
File "/Users/capstone1/lib/python3.8/site-packages/django/conf/__init__.py", line 64, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_FROM_EMAIL, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
所以我无法进行下一步 email.send()
根据错误描述,我想也许我应该使用 settings.py 中的 DJANGO_SETTINGS_MODULE 块,比如 -
DJANGO_SETTINGS_MODULE = [
{
'EMAIL_BACKEND':'django.core.mail.backends.smtp.EmailBackend',
'EMAIL_USE_TLS':True,
'EMAIL_PORT':587,
'EMAIL_HOST':"smtp.gmail.com",
'EMAIL_HOST_USER':'MYID@gmail.com',
'EMAIL_HOST_PASSWORD':'MYPASSWORD',
'SERVER_EMAIL':'MYID@gmail.com',
'DEFAULT_FROM_EMAIL':"MYID@gmail.com"
}
]
但它仍然给我同样的错误。
--
或者,如果我在终端上执行python manage.py shell,
那么,
>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('subject text', 'body text', to=['RECEIVINGEMAILID@gmail.com'])
>>> email.send()
我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/capstone1/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Users/capstone1/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "/Users/capstone1/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 62, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
我不明白为什么会收到连接被拒绝错误,因为我设置并启用了 Gmail 选项,或者为什么我在 python 控制台和 python 终端中出现不同的错误。
(+听说django已经安装了smtp,所以没做其他的。)
我该怎么办?
【问题讨论】:
标签: django email smtp gmail connection