我有一个filemanager应用程序,我希望在“baseusers”创建目录时通知某些特定的管理员用户。邮件将发送给管理员页面中定义的用户。最佳解决方案是在我的个人资料页面中有一个用于获取通知的复选框,但我希望首先使用此功能。这是我的简单解决方案:
from django.core.mail import send_mail, BadHeaderError from basedraft.sendmail.views import send_email from django.core.signals import request_finished @permission_required('fileman.can_fm_add') def createDir(request, path=None): if path is None: return HttpResponse(_(u"Path does not set.")) try: path = toString(path) os.mkdir(path) except Exception, msg: return raise_error(request, [str(msg)]) createHistory(request.user, "createdir", toString(path)) # Added this to history, added createdir to models # Send mail to recipients when a directory is created send_email(toString(path)) return HttpResponseRedirect('/fm/list/%s' % path)
问题在于网站会挂起,直到邮件发送完毕。我尝试使用这样的信号:
from django.core.mail import send_mail, BadHeaderError from basedraft.sendmail.views import send_email from django.core.signals import request_finished @permission_required('fileman.can_fm_add') def createDir(request, path=None): if path is None: return HttpResponse(_(u"Path does not set.")) try: path = toString(path) os.mkdir(path) except Exception, msg: return raise_error(request, [str(msg)]) createHistory(request.user, "createdir", toString(path)) # Added this to history, added createdir to models # Send mail to recipients when a directory is created #send_email(toString(path)) request_finished.connect(mail_callback) return HttpResponseRedirect('/fm/list/%s' % path) def mail_callback(sender, **kwargs): #path = setPath send_email('test')
但是这只是开始为每个完成的请求发送大量电子邮件。任何想法我应该怎么做,如果有一种方式,以便网站不会挂,但发送电子邮件是在后台完成的?
答案 0 :(得分:2)
要在后台发送和处理邮件,您应该使用邮件队列。
此主题讨论了几个选项:Advice on Python/Django and message queues
对于您的用例django-mailer似乎是一个不错的选择。