Django - 使用send_mass_mail()发送批量电子邮件

时间:2017-07-18 05:08:09

标签: django email

我尝试使用循环发送群发电子邮件,但它确实有用。但是,整个过程需要很长时间才能完成,并且收件人数量会增加。所以我决定使用send_mass_email()发送电子邮件。不幸的是我的代码似乎不起作用。我也找不到问题。请帮忙。

查看:

def noticeboard(request):
    title = "Notice Board"
    emaillists = []
    given_value = request.POST.get('radioGroup')
    if given_value == 'All':
        emaillist = MyUser.objects.all().values_list('email', flat=True)
        for email in emaillist:
            emaillists.append(str(email.encode('utf8')))
    if given_value == 'XYZ':
    .....
    .....

    form = noticeboardForm(request.POST or None)
    if form.is_valid():                
        FROM = "noticeboard@gmail.com"    
        SUBJECT = form.cleaned_data.get('subject').decode('utf-8')
        TEXT = form.cleaned_data.get('body').decode('utf-8')
        message = (SUBJECT, TEXT, FROM, emaillist)
        try:
            connection = get_connection()  
            connection.open()      
            send_mass_mail(message, fail_silently=False)
            connection.close() 
            print('successfully sent the mail')
        except:
            print("failed to send mail")            

        return redirect('delivery_success')

    return render(request, "noticeboardform.html", {"form": form, "title": title})


def delivery_success(request):
    return render(request, 'delivery_success.html')

1 个答案:

答案 0 :(得分:0)

Django docs说第一个参数应该是一个数据元组。

send_mass_mail((message,), fail_silently=False)

看看是否有效。