【问题标题】:TypeError: send_mail() takes exactly 3 arguments (1 given)TypeError:send_mail() 正好采用 3 个参数(给定 1 个)
【发布时间】: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


【解决方案1】:

正如上面的评论所说,您正在重新定义 send_mail。当我从插件电子邮件发送功能切换到本地 django 功能时,我陷入了这种混乱,并忘记了我原来的功能称为 send_mail。

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2015-07-03
    • 2019-04-05
    相关资源
    最近更新 更多