我在很多地方都看到过这个问题,但到目前为止还没有解决方案对我有用,或者我可能无法理解该怎么做(新手特征)。如果可以,请帮忙。
电子邮件设置/ settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'xyz@gmail.com'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
从Django Python Shell发送测试邮件时收到的错误:
首先我运行了send mail命令 - 给我错误1: django.core.exceptions.ImproperlyConfigured:
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'xyz@gmail.com', ['abc@hotmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site- packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 56, in send_mail
fail_silently=fail_silently)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 37, in get_connection
klass = import_string(backend or settings.EMAIL_BACKEND)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
克服错误1-我运行了以下命令
>>> from django.conf import settings
>>> settings.configure()
其次我再次发送邮件 - 给出错误2:电子邮件没有出去! socket.error:[Errno 111]拒绝连接
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'xyz@gmail.com', ['abc@hotmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/local/lib/python2.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 311, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 286, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
答案 0 :(得分:0)
所有这些当我试图从正常的python提示发送电子邮件时被调用为:
(venv)[user@server django_project]$ python
电子邮件从未发生过。实际上他们必须从
运行(venv)[user@server django_project]$ python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is test email', 'xyz@gmail.com', ['recipientemail@hotmail.com'], fail_silently=False)
这解决了我的问题。电子邮件完美无缺。所以我的电子邮件设置没有问题,我发送它的方式有问题。