django celery send multiple emails

2.3k views Asked by At

I have a view like"

def MyView(request):
    SendMyMail(args)

and my SendMymail is:

def SendMymail(args):
    send_mail(title, content, sender, receiver)

Here I want to queue the message sending process if there is so many messages. I have gone through document of celery and understood the base.

I have seen in many examples people are creating task.py file where they define the task with period. I want to know what to write in view then.

Can anyone give me clear example how can I accomplish of sending multiple emails

1

There are 1 answers

2
professorDante On BEST ANSWER

OK, I'll give you an example to get you going. Use celery to send a mail async.

def my_view(request):

    from tasks import celery_send_mail

    user = request.user
    <some_stuff>
    celery_send_mail.delay(user.email)
    <keep_going>
    return render(request)

then in tasks.py

@celery_app.task(ignore_result=True)
def celery_send_email(email):
    <send_your_mail>

To use celerybeat, you can set up the task to run periodically from your celery.conf file, or use a third party app to help, I use django-celery, as you can set the periodic tasks from the admin.