Celery異步發送郵件
阿新 • • 發佈:2019-01-21
image 編寫 inf worker set nbsp div work settings
一、編寫 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 12app = 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
Celery異步發送郵件