技术干货:django通过celery实现发送邮件的异步执行(内附代码)

释放双眼,带上耳机,听听看~!

文件源码如下:

from MxOnline.celery import app

@app.task
def send_register_email(email, send_type="register"):
    email_record = EmailVerifyRecord()
    if send_type == "update_email":
        code = random_str(4)
    else:
        code = random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()

    email_title = ""
    email_body = ""

    if send_type == "register":
        email_title = "慕学在线网注册激活链接"
        email_body = "请点击下面的链接激活你的账号: http://www.imooc.com/active/{0}".format(code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass
    elif send_type == "forget":
        email_title = "慕学在线网注册密码重置链接"
        email_body = "请点击下面的链接重置密码: http://www.imooc.com/reset/{0}".format(code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass
    elif send_type == "update_email":
        email_title = "慕学在线邮箱修改验证码"
        email_body = "你的邮箱验证码为: {0}".format(code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass

4. 编辑views.py文件完成邮件发送异步调用:

    #coding:utf-8
    from django.shortcuts import render
    from django.http import HttpResponse
    
    from .tasks import send_register_email
    
    def index(request):
        send_register_email.delay(“xxx@sina.com”)
        return HttpResponse(u"邮件发送成功, 请查收")

5. 进入MxOnline目录运行:
    celery -A MxOnline worker -l debug

   注: 
       1. 这里MxOnline 是指celery.py文件所在的package
       2. 目前cd到的目录应该是项目的根目录


    以此来启动celery的worker服务

【转自慕课】https://www.imooc.com

Python

数据结构-Python实现「选择类排序」

2022-3-3 10:31:30

Python

Python之方法调用

2022-3-3 10:38:22

搜索