Currently I have an Exchange server running and am attempting to run send_mail, though get a variety of errors.
Here is my current settings.py
EMAIL_HOST = 'exchange.domain.com'
EMAIL_HOST_USER = 'DOMAIN\user'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 443
EMAIL_USE_TLS = True
and when I run send_mail('''Subject''', '''Message.''', 'myemail#domain.com', ['otheremail#domain.com',], fail_silently=False)
after a very long wait
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
connection=connection).send()
File "/Library/Python/2.7/site-packages/django/core/mail/message.py", line 255, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 88, in send_messages
new_conn_created = self.open()
File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 49, in open
local_hostname=DNS_NAME.get_fqdn())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 311, in connect
(code, msg) = self.getreply()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 359, in getreply
+ str(e))
SMTPServerDisconnected: Connection unexpectedly closed: [Errno 54] Connection reset by peer
Related
I used Celery for email registration asynchronously, but when I trigger this asynchronous request, the following error will appear
Traceback (most recent call last):
File "d:\python3\lib\site-packages\dns\resolver.py", line 982, in nameservers
raise NotImplementedError
NotImplementedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\python3\lib\site-packages\celery\app\trace.py", line 451, in trace_task
R = retval = fun(*args, **kwargs)
File "d:\python3\lib\site-packages\celery\app\trace.py", line 734, in __protected_call__
return self.run(*args, **kwargs)
File "E:\Exploit_blog\blog_index_html\tasks.py", line 9, in send_email
send_mail(
File "d:\python3\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
return mail.send()
File "d:\python3\lib\site-packages\django\core\mail\message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "d:\python3\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "d:\python3\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "d:\python3\lib\smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "d:\python3\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "d:\python3\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "d:\python3\lib\smtplib.py", line 1040, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "d:\python3\lib\smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "d:\python3\lib\site-packages\eventlet\green\socket.py", line 44, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 543, in getaddrinfo
qname, addrs = _getaddrinfo_lookup(host, family, flags)
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 505, in _getaddrinfo_lookup
answer = resolve(host, qfamily, False, use_network=use_network)
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 450, in resolve
return _proxy.query(name, rdtype, raise_on_no_answer=raises,
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 394, in query
step(self._resolver.query, qname, rdtype, rdclass, tcp, source, raise_on_no_answer=False)
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 334, in _resolver
self.clear()
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 342, in clear
self._resolver = dns.resolver.Resolver(filename=self._filename)
File "d:\python3\lib\site-packages\dns\resolver.py", line 756, in __init__
self.read_registry()
File "d:\python3\lib\site-packages\dns\resolver.py", line 858, in read_registry
self.nameservers = info.nameservers
File "d:\python3\lib\site-packages\dns\resolver.py", line 984, in nameservers
raise ValueError(f'nameserver {nameserver} is not an '
ValueError: nameserver ; is not an IP address or valid https URL
There is no problem with the celery tasks code, because I was able to use it before, but the above situation appeared after I changed a computer
tasks code
from django.core.mail import send_mail
from celery import shared_task
# 发送邮件验证码,使用task进行异步处理,获取从视图层获取来的值,然后发送到用户邮箱中
#shared_task
def send_email(request, rand_str):
send_mail(
subject='验证码',
message='你的验证码为:' + rand_str + '请不要将你的验证码告诉其他人',
from_email='TEXT',
recipient_list=[request],
fail_silently=False
)
return (request, rand_str)
main settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxx'
EMAIL_HOST_PASSWORD = 'xxx'
.> celery exchange=celery(direct) key=celery
Celery startup console can recognize this tasks
[tasks]
. Let.tasks.add
. blog_index_html.tasks.send_email
When I searched for a few days, I determined the BUG location, it turned out that the eventlet and dnspython modules were incompatible, and I found a solution in his official GIT repository.
My operation is to uninstall and reinstall the dnspython module
github link
I'm sending emails using Django version 2.2 and have tested it on the local machine and it works. But the same code throws [Errno 97] Address family not supported by protocol exception.
I also couldn't find any solution online.
I have rechecked my code and there is nothing wrong in it.
I'm using google smtp server with app_password.
Below are Details.
settings.py file
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "*******#gmail.com"
EMAIL_HOST_PASSWORD = "app_password"
Email Sending Function
subject = 'Account Password reset mail'
ctx = {
"user" : self.user,
"website" : settings.WEBSITE_NAME,
"site_url" : settings.WEBSITE_URL,
}
msg = render_to_string('login/forgot_password_email.html',ctx)
from_email = settings.WEBSITE_ADMIN_EMAIL
to_email = [self.cleaned_data.get('email'),]
result = send_mail(subject, '', from_email, to_email, fail_silently=False, html_message=msg)
return result
I even try to login to server through SSH and execute through the shell but returns the same error.
>>> from django.core.mail import send_mail
>>> res = send_mail('Subject here', 'Here is the message.', 'example#gmail.com', ['example#gmail.com'], fail_silently=False,)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/lib/python3.7/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/home/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/opt/alt/python37/lib64/python3.7/socket.py", line 727, in create_connection
raise err
File "/opt/alt/python37/lib64/python3.7/socket.py", line 711, in create_connection
sock = socket(af, socktype, proto)
File "/opt/alt/python37/lib64/python3.7/socket.py", line 151, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
OSError: [Errno 97] Address family not supported by protocol
Thanks in Advance.
I checked Django app in Ubuntu 18.04 without ufw
When added to settings.py these:
EMAIL_HOST = 'smtp.timeweb.ru'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'myemail#timeweb.ru'
DEFAULT_FROM_EMAIL = 'myemail#timeweb.ru'
SERVER_EMAIL = 'myemail#timeweb.ru'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Run in the console
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('ddd','fffffffffff', 'myemail#timeweb.ru',['test#gmail.com'],fail_silently=False)
After 7 minutes I get this error.
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/mikhail/Django/Chapter01/mysite/env/lib/python3.7/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/home/mikhail/Django/Chapter01/mysite/env/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/mikhail/Django/Chapter01/mysite/env/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/mikhail/Django/Chapter01/mysite/env/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.7/smtplib.py", line 338, in connect
(code, msg) = self.getreply()
File "/usr/lib/python3.7/smtplib.py", line 394, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
What am I doing wrong?
I am trying to implement django-registration, but I get a gaierror on the registration page with the following traceback:
Internal Server Error: /accounts/register/
Traceback (most recent call last):
#irlevant
File "/usr/local/lib/python2.7/dist-packages/registration/views.py", line 36, in form_valid
new_user = self.register(form)
File "/usr/local/lib/python2.7/dist-packages/registration/backends/model_activation/views.py", line 29, in register
site=get_current_site(self.request)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 184, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/registration/models.py", line 97, in create_inactive_user
registration_profile.send_activation_email(site)
File "/usr/local/lib/python2.7/dist-packages/registration/models.py", line 182, in send_activation_email
self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 363, in email_user
send_mail(subject, message, from_email, [self.email], **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
return mail.send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 292, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known
Here is my settings.py:
DEBUG = True
ALLOWED_HOSTS = ['*']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '********#gmail.com'
EMAIL_HOST_PASSWORD = '*******'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
ACCOUNT_ACTIVATION_DAYS = 1
REGISTRATION_AUTO_LOGIN = True
SITE_ID = 1
LOGIN_REDIRECT_URL = '/'
Where should i check to find the mistake?
I faces the same problem, it was just an unstable internet connection because the problem disappeared as I changed my connection.
I was trying to send an email from my Django app using gmail. I added these email settings to settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
But it throws this name resolution error:
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.4/smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
self.source_address)
File "/usr/lib/python3.4/socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
Note: I am working behind a proxy.
In addition, I tried to connect to google.com through socket:
import socket
socket.gethostbyname('www.google.com')
It throws:
[Errno 11001] getaddrinfo failed
enable pop3 in gmail.com
create specific password for django application
(http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)
Solutions here