djangocelery异步发邮件

Posted ivy-blogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了djangocelery异步发邮件相关的知识,希望对你有一定的参考价值。

django、celery异步发邮件


 

  • django自带的send_mail发邮件功能执行发邮件功能会因为网络的原因造成花费的时间过长,为了解决这个问题,可以用celery + redis代替
  • 安装包:
    • pip install celery
    • pip install redis
  • 在django的根目录下新建celery_tasks文件夹,在该文件夹下新建tasks.py:
    •  1 from celery import Celery
       2 from django.conf import settings
       3 from django.core.mail import send_mail
       4 
       5 app = Celery(celery_tasks.tasks, broker=redis://127.0.0.1:6379/8)
       6 
       7 
       8 @app.task
       9 def send_register_active_email(email, username, token):
      10     subject = 测试
      11     message = ""
      12     from_email = settings.EMAIL_FROM
      13     recipient_list = [email]
      14     html_message = <h1>欢迎来到测试</h1>请点击下面链接激活<br><a href="http://http://127.0.0.1:8000/user/active?token=">点击此处激活</a>.format(
      15         username, token)
      16     send_mail(subject, message, from_email, recipient_list, html_message=html_message)

    • 调用该方法:
    •  1 class Register(View):
       2     def get(self, request):
       3         return render(request, user/register.html)
       4 
       5     def post(self, request):
       6         username = request.POST.get(username, ‘‘)
       7         password = request.POST.get(password, ‘‘)
       8         email = request.POST.get(email, ‘‘)
       9 
      10         # 校验用户名是否重复
      11         try:
      12             user = User.objects.get(username=username)
      13         except User.DoesNotExist:
      14             user = None
      15 
      16         if user:
      17             data = errmsg: 用户名已存在
      18             return JsonResponse(data)
      19         # try:
      20         #     user = User.objects.ger(email=email)
      21         # except User.DoesNotExist:
      22         #     user = None
      23         # if user:
      24         #     data = ‘errmsg‘: ‘邮箱已被注册‘
      25         #     return JsonResponse(data)
      26         user = User.objects.create_user(username=username, password=password, email=email)
      27         user.is_active = 0
      28         user.save()
      29 
      30         info = user_id: user.id
      31         serializer = Serializer(settings.SECRET_KEY, 3600 * 2)
      32         token = serializer.dumps(info).decode()
      33 
      34         # 发邮件
      35         send_register_active_email.delay(email, username, token)
      36         return redirect(reverse(user:login))

       

    • worker端配置:
      • 将django项目文件复制到另一个文件夹
      • 修改tasks.py文件,设置初始化
      •  1 from celery import Celery
         2 from django.conf import settings
         3 from django.core.mail import send_mail
         4 import os
         5 import django
         6 
         7 os.environ.setdefault(DJANGO_SETTINGS_MODULE, dream.settings)
         8 django.setup()
         9 
        10 app = Celery(celery_tasks.tasks, broker=redis://127.0.0.1:6379/8)
        11 
        12 
        13 @app.task
        14 def send_register_active_email(email, username, token):
        15     subject = 测试
        16     message = ""
        17     from_email = settings.EMAIL_FROM
        18     recipient_list = [email]
        19     html_message = <h1>欢迎来到测试</h1>请点击下面链接激活<br><a href="http://127.0.0.1:8000/user/active?token=">点击此处激活</a>.format(
        20         username, token)
        21     send_mail(subject, message, from_email, recipient_list, html_message=html_message)
      • worker端监听
      • 进入 worker端的代码的celery_tasks目录下:
        • celery -A tasks worker -l info
        • win10下可能报错:
          • pip install eventlet
          • celery -A tasks worker -l info -P eventlet
      • 当django启动调用send_mail的时候就会将发邮件的任务交给celery执行

以上是关于djangocelery异步发邮件的主要内容,如果未能解决你的问题,请参考以下文章

用python实现自动发邮件的功能

Django使用celery异步发邮件

ActivemMQ之消息服务器平台(发邮件)

Spring boot发电子邮件

异步发送邮件短信微信

Swoole来实现实时异步任务队列