Search code examples
pythondjangoemaildjango-email

Django-Email, sending multiple email depends on Email ID


Anyone know how to solved my issue, Im working with DJango-email with multiple recipient. Sending email in multiple recipient accounts from my DB are working, but now I want to send email and the email:body are depending on the Data ID.

This are the email list,

Scenario: Plate_No: 123123 will be send to [email protected] only and ABV112 will be send again to [email protected] and so on. Only the Plate_no assign in email will send, can someone help me to work my problem. Thank you!

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 <[email protected]>'
        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'

Solution

  • You can use a for-loop on your cstatus queryset to send the emails to the recipents. Did not test it, but it should look something like this:

    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 <[email protected]>'
        mail.send_mail(subject, plain_message, from_email, recipient_list, html_message=html_message, fail_silently=False)
        item.update(sent_email="Yes")