问题描述
任何人都知道如何解决我的问题,我正在使用多位收件人的 DJango 电子邮件。从我的数据库在多个收件人帐户中发送电子邮件可以正常工作,但现在我想发送电子邮件并且电子邮件:正文取决于数据 ID。
这是电子邮件列表,
场景:板号:123123 将仅发送到 example_email1@gmail.com,ABV112 将再次发送到 example_email2@gmail.com 等等。只有电子邮件中的 Plate_no 分配会发送,有人可以帮助我解决我的问题。谢谢!
class HomeView(ListView):
cstatus = VR.objects.filter(Deadline__date = datetime.datetime.today(),sent_email="No")
print(cstatus)
recipient_list = []
for recipient in cstatus:
recipient_list.append(recipient.email)
print(recipient_list)
plate = ""
for carreg in cstatus:
print(carreg.plate_no)
plate = carreg.plate_no
if plate != "":
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html',{'content':cstatus})
plain_message = strip_tags(html_message)
from_email = 'FMS <fms@gmail.com>'
mail.send_mail(subject,plain_message,from_email,recipient_list,html_message=html_message,fail_silently=False)
cstatus.update(sent_email="Yes")
model = VR
context_object_name = 'list'
template_name = 'vr/list.html'
解决方法
您可以在 cstatus 查询集上使用 for 循环将电子邮件发送给配方。没有测试它,但它应该看起来像这样:
for item in cstatus:
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html'{'content':item.Plate_no})
plain_message = item.Plate_no
recipent_list = [item.email]
from_email = 'FMS <fms@gmail.com>'
mail.send_mail(subject,plain_message,from_email,recipient_list,html_message=html_message,fail_silently=False)
item.update(sent_email="Yes")
,
根据我对您查询的理解,这可能是您需要的:
class HomeView(ListView):
cstatus = VR.objects.filter(Deadline__date = datetime.datetime.today(),sent_email="No")
print(cstatus)
recipient_list = {}
for recipient in cstatus:
recipient_list[recipient.plate_no] = recipient.email
print(recipient_list)
for carreg in cstatus:
print(carreg.plate_no)
plate = carreg.plate_no
if plate != "":
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html',{'content':carreg}) # or use plate for just plate_no
plain_message = strip_tags(html_message)
from_email = 'FMS <fms@gmail.com>'
mail.send_mail(subject,[recipient_list[plate]],fail_silently=False)
cstatus.update(sent_email="Yes")
model = VR
context_object_name = 'list'
template_name = 'vr/list.html'
或在 django 中使用群发电子邮件:
链接:https://docs.djangoproject.com/en/1.8/topics/email/#send-mass-mail
message1 = ('Subject here','Here is the message','from@example.com',['first@example.com','other@example.com'])
message2 = ('Another Subject','Here is another message',['second@test.com'])
send_mass_mail((message1,message2),fail_silently=False)
将上述所有消息结果添加到一个元组中,并将其添加到 send_mass_mail 中。例如。
datatuple = (
(subject,to_email),(subject,to_email)
) # to_mail -> recipient_list[plate]
send_mass_mail(datatuple)
如果我错了,请告诉我。