Issue with setting up email sender with Django 1.8 and Plesk - python

I'm trying to configure a website using Django 1.8 and Plesk, and setting the email variables to any value gets the traceback below. I've tried with Gmail and the email server from the host, and the error is the same. I have tested Python's smtplib and successfully sent an email through that, so it looks like it may be something between Django and Plesk. If I set up the website on a test CentOS 6 server, then I don't run into this problem.
Example Config settings
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_PASSWORD = "mypassword"
EMAIL_HOST_USER = "user#gmail.com"
EMAIL_PORT = 587
Traceback
mod_wsgi (pid=1703): Exception occurred processing WSGI script '/var/www/vhosts/s1085163.instanturl.net/oozoa.wsgi'.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/whitenoise/base.py", line 119, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 256, in handle_uncaught_exception
'request': request
File "/usr/local/lib/python2.7/logging/__init__.py", line 1185, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1278, in _log
self.handle(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1288, in handle
self.callHandlers(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1328, in callHandlers
hdlr.handle(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 751, in handle
self.emit(record)
File "/usr/local/lib/python2.7/site-packages/django/utils/log.py", line 129, in emit
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/usr/local/lib/python2.7/site-packages/django/utils/log.py", line 132, in send_mail
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 98, in mail_admins
mail.send(fail_silently=fail_silently)
File "/usr/local/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/site-packages/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 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/local/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known

Might be there is an issues with the firewall. Please try to disabled plesk firewall and IPtables on your server for the testing purpose and check it again.

Related

Celery has DNS resolution problems?

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

Can't send email with Django celery

I am try to send email in my django project using Celery and RabbitMQ.
Here is my project on GitHub https://github.com/Loctarogar/Django-by-Example.
In my tasks.py file typed admin#myshop.com but i tried change it to my real google mail and that have no effects.
Every time I have this Error:
[2017-11-20 20:09:36,747: ERROR/ForkPoolWorker-4] Task orders.tasks.order_created[a8fe13ed-08dc-4971-82ff-f184e197ab8d] raised unexpected: ConnectionRefusedError(111, 'Connection refused')
Traceback (most recent call last):
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/app/trace.py", line 374, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/app/trace.py", line 629, in __protected_call__
return self.run(*args, **kwargs)
File "/home/morilon/dj/shop/myshop/orders/tasks.py", line 14, in order_created
mail_sent = send_mail(subject, message, 'admin#myshop.com', [order.email])
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/message.py", line 348, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 104, in send_messages
new_conn_created = self.open()
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 64, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.5/smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.5/smtplib.py", line 306, in _get_socket
self.source_address)
File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I had exactly the same problem, but I solved it by setting up my settings.py just like skulegirl says, and just add this to my settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
I hope it helps you.
Your code repo is configured to use the console backend for emails, but your backtrace is showing the smtp backend is being used. I assume you've changed the backend and that's what's causing the error.
By default, the smtp backend looks for an smtp server on localhost. I'm guessing you don't have one running there.
If you want to send via gmail's smtp servers you can do so, you just need to configure the approriate settings in your settings.py file. See the django docs for what needs to be configured. Read the gmail docs for the exact smtp host address to use.

Python gmail imap/pop issues

I'm trying to write code to view messages from my g-mail, then search in the latest message for a certain phrase, that triggers a command.
But I'm having problems with connecting to my g-mail
from imapclient import IMAPClient
HOST = 'imap.gmail.com'
USERNAME = 'email'
PASSWORD = 'password'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
server.select_folder('INBOX')
print ('success')
I just added the 'success' at the end to make sure it worked, I'm using imap here but I tried pop which seems to be more confusing.
When I run this code i get this.
Traceback (most recent call last):
File "C:/Users/Yousef/Desktop/parser.py", line 8, in <module>
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 152, in __init__
self._imap = self._create_IMAP4()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 166, in _create_IMAP4
return imap4.IMAP4WithTimeout(self.host, self.port, self._timeout)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imap4.py", line 13, in __init__
imaplib.IMAP4.__init__(self, address, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\imaplib.py", line 197, in __init__
self.open(host, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\imaplib.py", line 294, in open
self.sock = self._create_socket()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imap4.py", line 16, in _create_socket
return socket.create_connection((self.host, self.port), self._timeout)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\socket.py", line 722, in create_connection
raise err
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
Someone please help me with this, because this is my first time using imap/pop.
when I put ssl to true, I get this...
Traceback (most recent call last):
File "C:\Users\Yousef\Desktop\parser.py", line 8, in <module>
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 152, in __init__
self._imap = self._create_IMAP4()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 164, in _create_IMAP4
self._timeout)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 171, in __init__
imaplib.IMAP4.__init__(self, host, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\imaplib.py", line 197, in __init__
self.open(host, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 177, in open
self.sock = wrap_socket(sock, self.ssl_context, host)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 144, in wrap_socket
ssl_context = create_default_context()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 127, in create_default_context
context.load_verify_locations(cadata=certs)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\backports\ssl\core.py", line 654, in load_verify_locations
self._ctx.load_verify_locations(cafile, capath)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\OpenSSL\SSL.py", line 669, in load_verify_locations
_raise_current_error()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
OpenSSL.SSL.Error: []
nvm i found a solution, thanks for the help anyways.

mail_admins causing connection refused for Django/Apache2?

I'm running Django with an Apache2 server on an Ubuntu backend. On load of my site, I get a 500 internal server error. The Apache log looks like:
AH00094: Command line: '/usr/sbin/apache2'
mod_wsgi (pid=9568): Exception occurred processing WSGI script '/home/ubuntu/project/project/wsgi.py'.
Traceback (most recent call last):
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site- packages/django/core/handlers/wsgi.py", line 177, in __call__
response = self.get_response(request)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 216, in get_response
'request': request
File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1321, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
self.emit(record)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/utils/log.py", line 117, in emit
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/utils/log.py", line 120, in send_mail
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 97, in mail_admins
mail.send(fail_silently=fail_silently)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-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 571, in create_connection
raise err
error: [Errno 111] Connection refused
This looks to me to be an error when the mail_admins method is called. I can post the contents of wsgi.py or other files if that is needed. My application involves no explicit email usage, so I'm not sure how this is happening. Any ideas? Thanks.
Django has a special logger for sending an email to admins when a 500-error occurs. Check out https://docs.djangoproject.com/en/1.9/topics/logging/#django.utils.log.AdminEmailHandler for more info.
It seems that an exception is thrown and django tries to propagate it by sending you an email, but the email cannot be send so you get another exception! Probably the cause of the real exception is before the exception that is caused by unable to send the email. However, I propose to fix your email configuration so that django will be able to send you these emails when a 500-error occurs, it is a nice feature (and a first step to a full exception tracker like Sentry). If, for some reason you are unable to send emails from that server then I recommend at least to configure your email backend (https://docs.djangoproject.com/en/1.9/topics/email/#email-backends) to either console (EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend') or file
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location
so that the mail backend will work.

Django-registration "gaierror at /accounts/register/ [Errno -2] Name or service not known"

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.

Categories