DjangoEmail,发送多封电子邮件取决于电子邮件ID

2024-09-20 22:54:28 发布

您现在位置:Python中文网/ 问答频道 /正文

任何人都知道如何解决我的问题,我与多个收件人DJango电子邮件工作。从我的数据库向多个收件人帐户发送电子邮件正在工作,但现在我想发送电子邮件,电子邮件:正文取决于数据ID

这是电子邮件列表

场景:车牌号123123将发送至示例_email1@gmail.com仅和ABV112将再次发送到示例_email2@gmail.com等等只有在电子邮件中没有分配的车牌才会发送,有人可以帮我解决我的问题。谢谢大家!

enter image description here

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'

Tags: comsend示例message电子邮件emailhtmlgmail
2条回答

根据我对您的查询的理解,这可能是您需要的:

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中的群发电子邮件:

链接: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)

如果我错了,请告诉我。

您可以在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")

相关问题 更多 >

    热门问题