0

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 example_email1@gmail.com only and ABV112 will be send again to example_email2@gmail.com 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 <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'
Adbul
  • 43
  • 1
  • 9

2 Answers2

1

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 <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")
barbaart
  • 837
  • 6
  • 14
0

According to what I understood regarding your query, this might what you need:

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'

Or Use mass emailing in 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)

Add all the above message results in a tuple and add it in send_mass_mail. For eg.

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

send_mass_mail(datatuple)

Let me know if I was wrong.

abdeali004
  • 463
  • 4
  • 9
  • Hi thank you. Im not using mass emailling since the recipient are not default its depends on user input. I tried the first one but I got an error " raise TypeError('"to" argument must be a list or tuple') TypeError: "to" argument must be a list or tuple" – Adbul May 17 '21 at 01:59
  • ohh sorry, I forgot to add brackets in the send_mail function. I edited it try it again. – abdeali004 May 17 '21 at 17:23