您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Django中怎么利用celery發(fā)送異步郵件,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
安裝依賴
pip install django-celery-beat pip install django-celery-email pip install celery pip install msgpack-python pip install msgpack
在settings文件中配置
添加app應(yīng)用到installed_apps中
"djcelery_email", "django_celery_beat"
修改.env文件配置:
#郵箱后端,使用celery email DJANGO_EMAIL_BACKEND=djcelery_email.backends.CeleryEmailBackend DJANGO_EMAIL_HOST=smtp.qq.com DJANGO_EMAIL_HOST_USER=xxx@qq.com # 您的郵箱號(hào) DJANGO_EMAIL_HOST_PASSWORD=你的郵箱密碼 DJANGO_DEFAULT_FROM_EMAIL=默認(rèn)發(fā)送的郵箱號(hào) #CELERY CELERY_BROKER_URL=redis://127.0.0.1:6379/1 #使用redis 1作為消息代理 CELERY_RESULT_BACKEND=redis://127.0.0.1:6379/2 #把任務(wù)結(jié)果放在redis2庫(kù)中
添加配置項(xiàng):
# Celery # ------------------------------------------------------------------------------ if USE_TZ: # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-timezone CELERY_TIMEZONE = TIME_ZONE # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-broker_url CELERY_BROKER_URL = env("CELERY_BROKER_URL") # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_backend CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND') # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-accept_content CELERY_ACCEPT_CONTENT = ["json", 'msgpack'] # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-task_serializer CELERY_TASK_SERIALIZER = "msgpack" # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_serializer CELERY_RESULT_SERIALIZER = "json" # http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-time-limit # TODO: set to whatever value is adequate in your circumstances CELERY_TASK_TIME_LIMIT = 5 * 60 # http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-soft-time-limit # TODO: set to whatever value is adequate in your circumstances CELERY_TASK_SOFT_TIME_LIMIT = 60 # http://docs.celeryproject.org/en/latest/userguide/configuration.html#beat-scheduler CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler" EMAIL_BACKEND = env( "DJANGO_EMAIL_BACKEND", default="djcelery_email.backends.CeleryEmailBackend" )
添加郵箱配置項(xiàng):
EMAIL_HOST = env('DJANGO_EMAIL_HOST', default="smtp.qq.com") EMAIL_USE_SSL = env('DJANGO_EMAIL_USE_SSL', default=True) EMAIL_PORT = env('DJANGO_EMAIL_PORT', default=465) EMAIL_HOST_USER = env('DJANGO_EMAIL_HOST_USER') EMAIL_HOST_PASSWORD = env('DJANGO_EMAIL_HOST_PASSWORD') DEFAULT_FROM_EMAIL = env('DJANGO_DEFAULT_FROM_EMAIL')
編寫(xiě)celery文件
在項(xiàng)目配置的目錄下新增celery_app.py文件
import os from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production") app = Celery("你的項(xiàng)目名") # Using a string here means the worker doesn't have to serialize # the configuration object to child processes. # - namespace='CELERY' means all celery-related configuration keys # should have a `CELERY_` prefix. app.config_from_object("django.conf:settings", namespace="CELERY") # Load task modules from all registered Django app configs. app.autodiscover_tasks()
在某app目錄下新增tasks.py文件:
from django.conf import settings from django.core.mail import send_mail from config import celery_app # 打上裝飾器celery就會(huì)自動(dòng)搜索到添加到任務(wù)中 @celery_app.task() def send_activity_verify_email(): """異步發(fā)送新訂單郵件""" title = '[學(xué)時(shí)通] 您有一個(gè)新活動(dòng)等待審核' message = '您有一個(gè)新活動(dòng)等待審核,請(qǐng)進(jìn)入學(xué)時(shí)通管理系統(tǒng)查看' send_mail(title, message=message, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=settings.EMAIL_RECIPIENT_LIST)
啟動(dòng)celery
來(lái)到項(xiàng)目目錄下:
執(zhí)行以下命令
其中config為我的項(xiàng)目配置目錄,config.celery_app指我項(xiàng)目配置目錄下的celery_app文件
$ /usr/local/python3/bin/celery -A config.celery_app worker -l info
當(dāng)點(diǎn)擊發(fā)送郵件時(shí),celery會(huì)把任務(wù)添加到redis 1庫(kù)中,郵箱發(fā)送完畢后,會(huì)在redis 2庫(kù)中顯示結(jié)果。
以上就是Django中怎么利用celery發(fā)送異步郵件,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。