I'm trying to set up self-serve password resets on a django setup. I have configured it to use the in-built password reset system, and have tested everything on my dev machine, and works fine. However, in production (i.e. actually sending emails), when I try to get a password reset email, it throws an SMTPServerDisconnected error:
[22/Sep/2017 15:04:59] ERROR [django.request:256] Internal Server Error: /password_reset/
Traceback (most recent call last):
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 145, in inner
return func(*args, **kwargs)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 110, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/contrib/auth/views.py", line 182, in password_reset
form.save(**opts)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 256, in save
html_email_template_name=html_email_template_name)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 213, in send_mail
email_message.send()
File "/srv/virtualenvs/sis_esv/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 "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 107, in send_messages
sent = self._send(message)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 123, in _send
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python2.7/smtplib.py", line 735, in sendmail
self.rset()
File "/usr/lib/python2.7/smtplib.py", line 469, in rset
return self.docmd("rset")
File "/usr/lib/python2.7/smtplib.py", line 394, in docmd
return self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
I'm using G-Suite (Google) as the SMTP relay; the settings I'm using are:
EMAIL_HOST = 'smtp-relay.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
The weird thing is I can send emails fine in other places within the system, using django's django.core.mail.send_mail() function.
The problem turned out to be that I hadn't set DEFAULT_FROM_EMAIL, thus django was attempting to send from a localhost email address, which Google's SMTP servers silently rejected. In other uses for send_mail() I had specified the from address, so this didn't appear as an issue there.
Related
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.
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.
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.
I have installed Open edX bitnami ami and testing it. I am facing problem with smtp email configuration. I followed the instructions here: Bitnami Open edX - How to configure outbound email settings
But that did not work. Email was not being sent and I could not trace the reason for this by seeing the log files. Then I followed the configurations here: Enable SMTP for EDX (Production Stack) and configured postfix with mandrill smtp mail service. From the server this command works:
sendemail -f registration#myedx.com -t mymail#gmail.com -m "Message Body" -u "Message Subject"
But form the edX instance I am getting error. the edx.log file looks like this:
Traceback (most recent call last):
File "/opt/bitnami/apps/edx/edx-platform/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/opt/bitnami/apps/edx/edx-platform/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
return view_func(*args, **kwargs)
File "/opt/bitnami/apps/edx/edx-platform/venv/lib/python2.7/site-packages/django/views/decorators/http.py", line 41, in inner
return func(request, *args, **kwargs)
File "/opt/bitnami/apps/edx/edx-platform/common/djangoapps/student/views.py", line 1890, in password_reset
domain_override=request.get_host())
File "/opt/bitnami/apps/edx/edx-platform/common/djangoapps/student/forms.py", line 72, in save
send_mail(subject, email, from_email, [user.email])
File "/opt/bitnami/apps/edx/edx-platform/venv/lib/python2.7/site-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/opt/bitnami/apps/edx/edx-platform/venv/lib/python2.7/site-packages/django/core/mail/message.py", line 248, in send
return self.get_connection(fail_silently).send_messages([self])
File "/opt/bitnami/apps/edx/edx-platform/venv/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 85, in send_messages
new_conn_created = self.open()
File "/opt/bitnami/apps/edx/edx-platform/venv/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 50, in open
self.connection.ehlo()
File "/opt/bitnami/python/lib/python2.7/smtplib.py", line 413, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "/opt/bitnami/python/lib/python2.7/smtplib.py", line 341, in putcmd
self.send(str)
File "/opt/bitnami/python/lib/python2.7/smtplib.py", line 333, in send
raise SMTPServerDisconnected('please run connect() first')
SMTPServerDisconnected: please run connect() first
Any idea how to solve this issue? Or how can I activate smtp email in bitnami edX instance?
I keep getting this intermittent error when trying to send through 'smtp.gmail.com'.
Traceback (most recent call last):
File "/var/home/ptarjan/django/mysite/django/core/handlers/base.py", line 92, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/var/home/ptarjan/django/mysite/registration/views.py", line 137, in register
new_user = form.save()
File "/var/home/ptarjan/django/mysite/registration/forms.py", line 79, in save
email=self.cleaned_data['email'])
File "/var/home/ptarjan/django/mysite/django/db/transaction.py", line 240, in _commit_on_success
res = func(*args, **kw)
File "/var/home/ptarjan/django/mysite/registration/models.py", line 120, in create_inactive_user
registration_profile.send_registration_mail()
File "/var/home/ptarjan/django/mysite/registration/models.py", line 259, in send_registration_mail
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
File "/var/home/ptarjan/django/mysite/django/core/mail.py", line 390, in send_mail
connection=connection).send()
File "/var/home/ptarjan/django/mysite/django/core/mail.py", line 266, in send
return self.get_connection(fail_silently).send_messages([self])
File "/var/home/ptarjan/django/mysite/django/core/mail.py", line 172, in send_messages
sent = self._send(message)
File "/var/home/ptarjan/django/mysite/django/core/mail.py", line 186, in _send
email_message.message().as_string())
File "/usr/lib/python2.5/smtplib.py", line 704, in sendmail
(code,resp) = self.data(msg)
File "/usr/lib/python2.5/smtplib.py", line 484, in data
(code,repl)=self.getreply()
File "/usr/lib/python2.5/smtplib.py", line 352, in getreply
line = self.file.readline()
File "/usr/lib/python2.5/smtplib.py", line 160, in readline
chr = self.sslobj.read(1)
sslerror: The read operation timed out
I'm running Django 1.1 with the django-registarion app. With these in my settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "**SECRET**"
EMAIL_HOST_PASSWORD = "**SECRET**"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Altho' I don't know why, I have been thro' this, and it works when you have settings variables ordered in a particular order:
EMAIL_HOST
EMAIL_PORT
EMAIL_HOST_USER
EMAIL_HOST_PASSWORD
EMAIL_USE_TLS
Looks like gmail may simply be occasionally slow to respond, so your operation times out. Perhaps you can use a try/except to catch such issues and retry a few times (maybe waiting a while between attempts). BTW, this task seems well suited to a dedicated or pooled thread which encapsulates the whole "send mail with retries" operation.