Django-Email,发送多封邮件取决于Email ID

Django-Email, sending multiple email depends on Email ID

任何人都知道如何解决我的问题,我正在与多个收件人一起使用 DJango 电子邮件。从我的数据库在多个收件人帐户中发送电子邮件是有效的,但现在我想发送电子邮件并且 email:body 取决于数据 ID。

这是电子邮件列表,

场景: Plate_No: 123123 将仅发送至 example_email1@gmail.com,ABV112 将再次发送至 example_email2@gmail.com 等等。只有电子邮件中的 Plate_no 分配会发送,有人可以帮助我解决我的问题。谢谢!

auto send email script:

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, plain_message, from_email, [recipient_list[plate]], html_message=html_message, fail_silently=False)
            cstatus.update(sent_email="Yes")

    model = VR
    context_object_name = 'list'
    template_name = 'vr/list.html'

或者在 django 中使用群发邮件:

link: 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', 'from@example.com', ['second@test.com'])
send_mass_mail((message1, message2), fail_silently=False)

将上述所有消息结果添加到一个元组中,并将其添加到send_mass_mail中。例如

datatuple = (
    (subject, plain_message, from_email, to_email),
    (subject, plain_message, from_email, to_email)
) # to_mail -> recipient_list[plate]

send_mass_mail(datatuple)

如果我错了请告诉我。