Python:SSL读取时电子邮件发送失败

时间:2009-09-03 23:15:54

标签: python django email smtp ssl

尝试通过'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

我正在使用django-registarion应用程序运行Django 1.1。在我的settings.py

中使用这些
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "**SECRET**"
EMAIL_HOST_PASSWORD = "**SECRET**"
EMAIL_PORT = 587
EMAIL_USE_TLS = True

2 个答案:

答案 0 :(得分:3)

Altho'我不知道为什么,我已经成为了这个,并且当您按特定顺序排序设置变量时它会起作用:

  • EMAIL_HOST
  • EMAIL_PORT
  • EMAIL_HOST_USER
  • EMAIL_HOST_PASSWORD
  • EMAIL_USE_TLS

答案 1 :(得分:0)

看起来gmail可能偶尔会很慢响应,因此您的操作会超时。也许您可以使用try / except来捕获此类问题并重试几次(可能在尝试之间等待一段时间)。顺便说一下,这个任务似乎很适合专用或池化的线程,它封装了整个“重试发送邮件”操作。