问题描述
我试图设置一个超时时间来发送Django的电子邮件。我使用的是django 1.7.3和python v2.7.6。我的aproach遵循中的django文档。所以我做的是通过在django / core / mail / backends文件夹上创建一个名为myemailbackend.py的文件来创建一个自定义邮件后端,其代码如下:
I'm trying to set a timeout for sending email with Django. I'm using django 1.7.3 and python v2.7.6. My aproach was follow the django documentation in here. 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:
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)
之后在我的settings.py中,我设置了我的EMAIL_BACKEND:
After that in my settings.py i set my 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.
在runserver之后,我注意到这似乎不起作用,我注意到myemailbackend .py没有编译。
我失踪了什么?如何设置发送电子邮件的超时时间?
After i runserver i've noticed that this doesn't seem to work, i notice to that myemailbackend.py didn't was compile.What am i'm missing? How can i set a timeout for send email, afterall?
推荐答案
如果它被命名为 myemailbackend.py
在文件夹 django / core / mail / backends
中,那么你的设置将是
If it is named myemailbackend.py
in the folder django/core/mail/backends
, then your setting would be
EMAIL_BACKEND = 'django.core.mail.backends.myemailbackend.MyEmailBackend'
$ b $那么说,把你的代码放到Django文件夹里是个坏主意。最好把它放在一个应用程序(比如说, my_app / mymailbackend.py
)中,以免Django重新安装和/或升级。
that being said, it is a bad idea to place your code into a Django folder. 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.
这篇关于如何设置使用django发送电子邮件的超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!