使用 send_mass_mail 创建用于发送大量电子邮件 Django 的动态元组
Creating Dynamic Tuple for Sending Mass Email Django using send_mass_mail
我看到很多文档和示例来创建群发电子邮件,如下所示
message1 = ('That’s your subject #1',
'That’s your message body #1',
'from@yourdjangoapp.com',
['to@yourbestuser1.com', 'to@yourbestuser2.com'])
message2 = ('That’s your subject #2',
'That’s your message body #2',
'from@yourdjangoapp.com',
['to@yourbestuser2.com'])
message3 = ('That’s your subject #3',
'That’s your message body #3',
'from@yourdjangoapp.com',
['to@yourbestuser3.com'])
send_mass_mail((message1, message2, message3), fail_silently=False)
我想向 N 个从我的查询集中返回的用户发送相同消息的群发电子邮件。
我正在使用下面的代码发送大量电子邮件。但是,它非常慢,想知道是否存在任何有效的方法,或者因为我使用 gmail 进行测试,它会很慢。
def sendMassPasswordResetEmail(request):
schoolval = user_school.objects.filter(username=request.user.id).values_list('schoolCode',flat=True)[0]
clsval = student_group.objects.filter(schoolCode=schoolval).values_list('classVal_id').distinct()
student = studentclass.objects.all().prefetch_related('student_group_set__username').filter(student_group__classVal__in=clsval).distinct()
addStudentlink = True
#groups = student.values('student_group')
userinschool = User.objects.filter(user_school__schoolCode=schoolval,user_type=1).values('username')
html_content = render_to_string('dashboard/registration_email.html') # render with dynamic value
text_content = strip_tags(html_content) # Strip the html tag. So people ca
for recipient in userinschool:
subject, from_email, to = 'Account Activation xxxxx', 'xxxxxgmail.com.com',recipient.get('username')
print(recipient.get('username'))
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
return render(request,'dashboard/add-student.html',{'students':student, 'addStudentlink':addStudentlink})
我无法为 send_mass_email 生成(由于缺乏知识)动态元组。任何帮助将不胜感激。
您应该使用完全符合您要求的 send_mass_mail。
这是一个如何操作的例子:
messages = ()
for item in items:
messages = messages + ((item.subject, item.message, item.from, [item.to]),)
send_mass_mail(messages, fail_silently=False)
我看到很多文档和示例来创建群发电子邮件,如下所示
message1 = ('That’s your subject #1',
'That’s your message body #1',
'from@yourdjangoapp.com',
['to@yourbestuser1.com', 'to@yourbestuser2.com'])
message2 = ('That’s your subject #2',
'That’s your message body #2',
'from@yourdjangoapp.com',
['to@yourbestuser2.com'])
message3 = ('That’s your subject #3',
'That’s your message body #3',
'from@yourdjangoapp.com',
['to@yourbestuser3.com'])
send_mass_mail((message1, message2, message3), fail_silently=False)
我想向 N 个从我的查询集中返回的用户发送相同消息的群发电子邮件。 我正在使用下面的代码发送大量电子邮件。但是,它非常慢,想知道是否存在任何有效的方法,或者因为我使用 gmail 进行测试,它会很慢。
def sendMassPasswordResetEmail(request):
schoolval = user_school.objects.filter(username=request.user.id).values_list('schoolCode',flat=True)[0]
clsval = student_group.objects.filter(schoolCode=schoolval).values_list('classVal_id').distinct()
student = studentclass.objects.all().prefetch_related('student_group_set__username').filter(student_group__classVal__in=clsval).distinct()
addStudentlink = True
#groups = student.values('student_group')
userinschool = User.objects.filter(user_school__schoolCode=schoolval,user_type=1).values('username')
html_content = render_to_string('dashboard/registration_email.html') # render with dynamic value
text_content = strip_tags(html_content) # Strip the html tag. So people ca
for recipient in userinschool:
subject, from_email, to = 'Account Activation xxxxx', 'xxxxxgmail.com.com',recipient.get('username')
print(recipient.get('username'))
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
return render(request,'dashboard/add-student.html',{'students':student, 'addStudentlink':addStudentlink})
我无法为 send_mass_email 生成(由于缺乏知识)动态元组。任何帮助将不胜感激。
您应该使用完全符合您要求的 send_mass_mail。
这是一个如何操作的例子:
messages = ()
for item in items:
messages = messages + ((item.subject, item.message, item.from, [item.to]),)
send_mass_mail(messages, fail_silently=False)