# 来完成异步发送邮件

import time
from django.core.mail import send_mail
from django.conf import settings
from celery import Celery

# 初始化django环境
import django
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','clmwm.settings.develop')
django.setup()

# 创建实例对象
# 第一个parameter 为本文件所在路径
# broker 指定中间人，斜杠后指定第几个数据库
app = Celery('celery_execute_task.sendmail',broker='redis://192.168.1.116:8379/3')

# 定义任务函数
@app.task
def send_activate_email(to_email,username,token):
    """发送激活邮件"""

    # 组织邮件信息
    subject = 'clmwm欢迎您'
    message = ''
    sender = settings.EMAIL_FROM
    receiver = [to_email]
    html_message = '<h1>%s,欢迎您注册吃了么会员</h1>请点击下面链接激活您的账户' \
                   '<br><a href="http://127.0.0.1:8000/user/activate/%s">' \
                   'http://127.0.0.1:8000/user/activate/%s</a>'%(username,token,token)
    send_mail(subject,message,sender,receiver,html_message=html_message)
    time.sleep(5)
