2017-07-18 86 views
1

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

视图:

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 = "[email protected]"  
     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') 

回答

0

Django文档说,第一个参数是一个数据元组。

send_mass_mail((message,), fail_silently=False) 

看看是否有效。

+0

请检查是否属实。写一个'print'语句,看它是否打印任何东西。你会知道代码是否进入'is_valid()'块。 – MiniGunnR

+0

它的工作原理。我的错。我的电子邮件正文没有被收集。因此错误。 – surajitM

+0

谢谢@MiniGunnR。 :) – surajitM