Paul Tarjan
Paul Tarjan

Reputation: 50602

Python : email sending failing on SSL read

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

Upvotes: 1

Views: 2137

Answers (2)

lprsd
lprsd

Reputation: 87095

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

Upvotes: 3

Alex Martelli
Alex Martelli

Reputation: 881655

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.

Upvotes: 0

Related Questions