一、编写 tasks.py
在项目根目录下创建包和py文件
发送邮件代码如下
1 from django.core.mail import send_mail 2 from django.conf import settings 3 from celery import Celery 4 5 6 # 在任务处理者一端加这几句 7 import os 8 import django 9 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dailyfresh.settings") 10 django.setup() 11 12 app = Celery(‘celery_tasks.tasks‘, broker=‘redis://127.0.0.1:6379/8‘) 13 14 15 @app.task 16 def send_register_active_email(to_email, username, token): 17 """发送激活邮件""" 18 subject = ‘天天生鲜欢迎信息‘ 19 message = ‘‘ 20 sender = settings.EMAIL_FROM 21 receiver = [to_email] 22 html_message = ‘<h1>%s, 欢迎您成为天天生鲜注册会员</h1>请点击下面链接激活您的账户<br/><a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>‘ % (username, token, token) 23 24 send_mail(subject, message, sender, receiver, html_message=html_message)
二、在 views.py 调用
导入模块
调用
三、启动服务
# 启动 Redis redis-server # 启动 Celery (先进入到项目根目录下) celery -A celery_taskes.tasks worker -l info
原文地址:https://www.cnblogs.com/mxsf/p/10296471.html
时间: 2024-10-09 07:30:19