How to attach files with celery args?

50 views Asked by At

I have been facing problem regarding how to attach the file in the celery args. I am creating an email schedule functionality in my website where I am using django-celery-beat to do this. However, the file is OfCourse user uploaded, and optional, and for the args in PeriodicTask model of django-celery-beat, I can't really get the file to be sent along with the email.

this is the function (not in views.py)

def send_scheduled_email(to_email_list_final,cc_email_list_final,bcc_email_list_final,subject,mail_body,email_schedule_date_time,attachment=None):

scheduled_email_date_time = datetime.strptime(email_schedule_date_time, '%Y-%m-%dT%H:%M')
unique_task_name = f"{subject}_{scheduled_email_date_time.timestamp()}"

PeriodicTask.objects.create(
clocked = ClockedSchedule.objects.get_or_create(clocked_time=scheduled_email_date_time)[0],
                            name=unique_task_name ,
                            task = "public_relation_team.tasks.send_scheduled_email",
                            args =json.dumps([to_email_list_json,cc_email_list_json,bcc_email_list_json,subject,mail_body,attachment]),
                            one_off = True,
                            enabled = True,
                        )

And this is in tasks.py

@shared_task
def send_scheduled_email(to_email_list, cc_email_list, bcc_email_list, subject, mail_body, attachment):

PRT_Email_System.send_email(to_email_list, cc_email_list, bcc_email_list, subject, mail_body,attachment)

For emails without being scheduled, i.e that can be send directly I have used file attachment system and faced no problem with it This is the function,

def send_email_confirmation(to_email_list_final,cc_email_list_final,bcc_email_list_final,subject,mail_body,attachment):
            email_from = settings.EMAIL_HOST_USER 
            if attachment is None:
                try:
                    email=EmailMultiAlternatives(subject,mail_body,
                            email_from,
                            to_email_list_final,
                            bcc=bcc_email_list_final,
                            cc=cc_email_list_final
                            )
                    email.send()
                    return True
                except Exception as e:
                    print(e)
                    return False    
            else:
                try:
                    #Create a ContentFile from the uploaded file
                    content_file = ContentFile(attachment.read())
                    content_file.name = attachment.name  # Set the filename
                    email=EmailMultiAlternatives(subject,mail_body,
                            email_from,
                            to_email_list_final,
                            bcc=bcc_email_list_final,
                            cc=cc_email_list_final
                            )
                    email.attach(attachment.name,content_file.read(),attachment.content_type)
                    email.send()
                    return True
                except Exception as e:
                    print(e)
                    return False

I want to use this to send the file attached email as well for the scheduled mail. Please help!

I have tried all possible solutions but i am still stuck

0

There are 0 answers