在settings.py中設置html
EMAIL_USE_SSL = Truepython
EMAIL_HOST = 'smtp.qq.com' # 若是是 163 改爲 smtp.163.comredis
EMAIL_PORT = 465sql
EMAIL_HOST_USER = '763005825@qq.com' # 賬號django
EMAIL_HOST_PASSWORD = 'jffbcdknmbzvbega' # 密碼json
DEFAULT_FROM_EMAIL = EMAIL_HOST_USERsession
引入包函數
from django.core.mail import send_mailpost
發送郵件網站
send_mail('主題',"內容",'發送帳號',['接收帳號'], fail_silently=False)
selery發送郵件
celery
名詞
使用
celery==3.1.25
celery-with-redis==3.0
django-celery==3.1.17
INSTALLED_APPS = (
...
'djcelery',
}
...
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://127.0.0.1:6379/0'
CELERY_IMPORTS = ('應用名稱.task')
from celery import task
from django.core.mail import send_mail
import time
@task
def sendmail():
email='763005825@qq.com'
send_mail('reigster',"歡迎登陸每天生鮮",'763005825@qq.com',[email],fail_silently=False)
time.sleep(10)
python manage.py migrate
sudo redis-server /etc/redis/redis.conf
python manage.py celery worker --loglevel=info
#from task import *
def index(request):
sendmail.delay()
return HttpResponse("hello world")
# 專門進行驗證def validate_sql(data): for k,val in data: if re.match("['select' ,'update','delete']", val) != None: return False return True# 註冊class Register(View): def get(self,request): return render(request,'index/register.html') def post(self,request): mes = {} data=request.POST # flag=validate_sql(data) # if flag==False: # mes['code'] = 10015 # mes['message'] = '輸入不合法' # return HttpResponse(json.dumps(mes)) username=data.get('user_name') pwd=data.get('pwd') cpwd=data.get('cpwd') email=data.get('email') allow=data.get('allow') if not all([username,pwd,cpwd,email,allow]): mes['code']=10010 mes['message']='都不能爲空' return HttpResponse(json.dumps(mes)) else: # 用戶名必須爲8-12位字母 if re.match("\w{8,12}$",username)==None: mes['code']=10011 mes['message']='用戶名不合法' # if re.match("['select' ,'update','delete']",username)!=None: # mes['code']=10012 # 驗證郵箱 if re.match("\w{8,12}@(163|126|qq).com$", email) == None: mes['code'] = 10012 mes['message'] = '用戶名不合法' return HttpResponse(json.dumps(mes)) # 驗證兩次輸入的密碼是否一致 if pwd!=cpwd: mes['code'] = 10013 mes['message'] = '兩次輸入的密碼不一致' return HttpResponse(json.dumps(mes)) # 註冊 user=User(name=username,password=make_password(pwd), token='23') try: user.save() request.session['indexuser']=username # 發送郵件 from mypro.settings import DEFAULT_FROM_EMAIL send_mail('你好,歡迎註冊',"歡迎你註冊xx網站<a href='/authuser?" "token=wertfgb>",DEFAULT_FROM_EMAIL,[email],fail_silently=False) except: mes['code'] = 10018 mes['message'] = '添加失敗' return HttpResponse(json.dumps(mes)) mes['code']=1200 mes['message']='' return HttpResponse(json.dumps(mes))