【问题标题】:Django-Email, sending multiple email depends on Email IDDjango-Email,发送多封邮件取决于Email ID
【发布时间】:2021-05-12 05:19:35
【问题描述】:

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

这是电子邮件列表,

场景:Plate_No: 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'

【问题讨论】:

    标签: python django email django-email


    【解决方案1】:

    您可以在您的 cstatus 查询集上使用 for 循环将电子邮件发送给 recipents。没有测试它,但它应该看起来像这样:

    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")
    

    【讨论】:

    • 感谢我的朋友提供的出色解决方案。
    【解决方案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)
    

    如果我错了,请告诉我。

    【讨论】:

    • 您好,谢谢。我没有使用群发电子邮件,因为收件人不是默认的,这取决于用户输入。我尝试了第一个,但出现错误“ raise TypeError('"to" argument must be a list or tuple') TypeError: "to" argument must be a list or tuple"
    • 哦,对不起,我忘了在 send_mail 函数中添加括号。我编辑了它再试一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2018-09-28
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多