【发布时间】:2016-05-19 17:03:27
【问题描述】:
我的 Django 项目中有一个应用程序,如果温度超过最高温度,它会在运行服务器时异步发送电子邮件。实际上,作为开始,我只发送给一个收件人,并且我将从 foo 函数接收到的温度与一个常数进行比较。
但它既没有异步发送电子邮件,也没有在我访问此应用程序 url 时返回错误消息:
TypeError: send_mail() 只接受 3 个参数(给定 1 个)
邮件/views.py
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from threading import Thread
from cap import foo
class EmailThread(Thread):
def __init__(self, subject, content, from_email):
self.subject = request.POST.get('subject', 'subject')
self.content = request.POST.get('content', 'attention ! la temperature a depasse le maximum ')
self.from_email = request.POST.get('from_email', '*****@gmail.com')
Thread.__init__(self)
def run(self):
if subject and content and from_email:
try:
send_mail(subject, content, from_email, [ '******@gmail.com' ])
return HttpResponse('templates/mail.html')
except BadHeaderError:
return HttpResponse('Invalid header found.')
return HttpResponseRedirect('mail')
else:
return HttpResponse('Make sure all fields are entered and valid.')
def send_mail(subject, content, recipient):
x = foo()
if x >= 12 :
EmailThread(subject, content, recipient).start()
【问题讨论】:
-
您正在重新定义
send_mail并且可能会在其他地方破坏它,因为您是从django.core.mail导入它并自己定义它
标签: python django multithreading email