Bulkmailer / BulkMailer-Backend

Backend for Bulk Mailer
https://bulkmailer.suhaila.tech
7 stars 4 forks source link

Fix bulk Mailer Logic #1

Open ClawedCatalyst opened 1 year ago

ClawedCatalyst commented 1 year ago

fix bulk mailer logic

Current Logic

  for recipient in datatuple:
        msg = EmailMultiAlternatives(
            _subject, _body, by, [recipient["email"]], connection=connections
        )
        if _template is not None and _template != "null":
            formatedhtml = html
            sending_string = ""
            if re.findall("{{name}}", html_string):
                formatedhtml = html.render(Context({"name": recipient["name"]}))
                sending_string = formatedhtml + ""
            msg.attach_alternative(sending_string, "text/html")

        if file_attached.count() > 0:
            for file in file_attached:
                msg.attach_file(settings.MEDIA_ROOT + (f"/{file.file.name}"))
        messages.append(msg)
    return connections.send_messages(messages)

Threading and Queue based logic

custom_mail_queue = Queue()

def send_custom_mails():
     while not custom_mail_queue.empty():
        email = custom_mail_queue.get()        
        emailContent =  EmailMultiAlternatives(email['subject'],email['plain_text_content'],settings.EMAIL_HOST, [email['email']])
        html_template = Template(str(TemplateModel.objects.get(id=email['html_content']).html_content))
        formatted_html = html_template.render(Context({'article_url':email['article_url'], 'preview_text':email['preview_text'], 'plain_text_content':email['plain_text_content']})) 
        emailContent.attach_alternative(formatted_html + '', "text/html")

        emailContent.send() 

@shared_task(bind=True)
def custom_mail(self, Subject, preview_text, article_url, html_content, plain_text_content):
    num_cores = multiprocessing.cpu_count()
    max_worker_threads = num_cores * 2 

    activeUsers = User.objects.filter(is_active=True)
    for user in activeUsers:
        email_structure = {
            'email':user.email,
            'subject':Subject,
            'preview_text':preview_text,
            'article_url':article_url,
            'html_content':html_content,
            'plain_text_content':plain_text_content
        }
        custom_mail_queue.put(email_structure)

    for _ in range(max_worker_threads):
        workerThread = Thread(target=send_custom_mails)
        workerThread.daemon = True
        workerThread.start()
jainendra001 commented 11 months ago

Hi @ClawedCatalyst I am really interested to work on this issue, I am currently a fullstack developer pursuing undergrad at IIT's , Could you please assign me this issue.

ClawedCatalyst commented 11 months ago

Yes, @jainendra001, you can start working on this. If you need any help regarding the issue or setup, please feel free to contact me on Discord. You can find the Discord invite link in the Community Support section of the readme.