简体   繁体   English

如何设置使用 django 发送电子邮件的超时时间?

[英]How to set Timeout for sending email using django?

I'm trying to set a timeout for sending email with Django.我正在尝试设置使用 Django 发送电子邮件的超时时间。 I'm using django 1.7.3 and python v2.7.6.我正在使用 django 1.7.3 和 python v2.7.6。 My aproach was follow the django documentation in here .我的方法是按照此处的 django 文档进行操作。 So what i did was create a custom email backend by creating a file named myemailbackend.py on django/core/mail/backends folder with the following code:所以我所做的是通过使用以下代码在 django/core/mail/backends 文件夹上创建一个名为 myemailbackend.py 的文件来创建一个自定义电子邮件后端:

from django.core.mail.backends import smtp

class MyEmailBackend(smtp.EmailBackend):
  def __init__(self, *args, **kwargs):
      kwargs.setdefault('timeout', 3) #this is 3 secs, i believe.
      super(MyEmailBackend, self).__init__(*args, **kwargs)

After that in my settings.py i set my EMAIL_BACKEND:之后在我的 settings.py 中我设置了我的 EMAIL_BACKEND:

# Email setup

EMAIL_BACKEND = 'django.core.mail.backends.base.myemailbackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxx'
EMAIL_PORT = 587
# EMAIL_TIMEOUT = 3  # 3 sec, this would be great but i notice that this is not possible since that backend stmp.py doesn't expect to get "EMAIL_TIMEOUT" var.

After i runserver i've noticed that this doesn't seem to work, i notice to that myemailbackend.py didn't was compile.在我运行服务器后,我注意到这似乎不起作用,我注意到 myemailbackend.py 没有被编译。 What am i'm missing?我错过了什么? How can i set a timeout for send email, afterall?毕竟,如何设置发送电子邮件的超时时间?

Django 有一个设置: EMAIL_TIMEOUT

If it is named myemailbackend.py in the folder django/core/mail/backends , then your setting would be如果它在文件夹django/core/mail/backends被命名为myemailbackend.py ,那么您的设置将是

EMAIL_BACKEND = 'django.core.mail.backends.myemailbackend.MyEmailBackend'

that being said, it is a bad idea to place your code into a Django folder.话虽如此,将您的代码放入 Django 文件夹是一个坏主意。 It is better to place this in an app (say, my_app/mymailbackend.py ) so that it will not be affected by Django reinstalls and/or upgrades.最好将它放在一个应用程序中(比如my_app/mymailbackend.py ),这样它就不会受到 Django 重新安装和/或升级的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM