django+celery+redis异步发送邮件
代码如下
tasks.py
from celery import Celery
from shiyanlou.settings import *
from django.conf import settings
from django.core.mail import send_mail
app = Celery('celery_task.tasks',broker="redis://127.0.0.1:6379/3",worker="redis:127.0.0.1:6379/4")
@app.task()
def send_mail_task(email):
subject = "主题"
message=''
sender = EMAIL_FROM
receiver = [email]
html_message='<h1>hi<h1> thank you'
send_mail(subject,message,sender,receiver,html_message=html_message)
post.py
from django.core.mail import send_mail
from itsdangerous import TimedJSONWebSignatureSerializer
from celery_tasks.tasks import send_mail_task
class Bbb(APIView):
def get(self,request):
email = '2040913671@qq.com'
send_mail_task(email)
return Response({'code':200})
邮箱配置
"""
邮箱配置
EMAIL_HOST_PASSWORD 为邮箱smtp验证码,需到邮箱去获取权限
EMAIL_HOST 绑定服务器,可以百度查看需要的smtp
"""
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com'
# EMAIL_USE_TLS = False
EMAIL_PORT = 465
EMAIL_USE_SSL = True
# 发送邮件的邮箱
EMAIL_HOST_USER = '***@qq.com'
# 在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = '*****'
# 收件人看到的发件人
EMAIL_FROM = '***@qq.com'
celery启动指令:
celery -A celery_tasks.tasks worker -l info
代码仅供参考