简体   繁体   中英

How to set Timeout for sending email using django?

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)

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.

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?

Django 有一个设置: EMAIL_TIMEOUT

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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