qq獲取驗證碼接口


測試 獲取驗證碼 

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

#定義參數
my_mail = "2501186626@qq.com"
#授權碼
my_pass = "qnxrmxwxlyzoeadc"


#定義發送郵件的方法
def mail(subject,content,mailaddr):

    #聲明郵件對象
    msg = MIMEText(content,'plain','utf-8')

    #設置發送方對象
    msg['From'] = formataddr(['在線教育平台',my_mail])

    #設置收件方對象
    msg['To'] = formataddr(['尊敬的客戶',mailaddr])

    #設置標題
    msg['Subject'] = subject

    #設置smtp服務器
    server = smtplib.SMTP_SSL("smtp.qq.com",465)

    #登錄郵箱
    server.login(my_mail,my_pass)

    #發送郵件
    server.sendmail(my_mail,[mailaddr],msg.as_string())

    #關閉smtp鏈接
    server.quit()


mail('驗證碼驗證服務','您的驗證碼是6666,有限期為2分鍾','164850527@qq.com')

  獲取qq驗證碼接口

class Main(APIView):
	def post(self,request):
		my_mail=request.data.get('email')
		mailaddr=my_mail
		my_pass = "qnxrmxwxlyzoeadc"
		subject='驗證碼驗證服務'
		import random
		content='%06d' % random.randint(0, 999999)  # 隨機6位驗證碼
		msg = MIMEText(content, 'plain', 'utf-8')
		# 設置發送方對象
		msg['From'] = formataddr(['在線教育平台', my_mail])

		# 設置收件方對象
		msg['To'] = formataddr(['尊敬的客戶', mailaddr])

		# 設置標題
		msg['Subject'] = subject

		# 設置smtp服務器
		server = smtplib.SMTP_SSL("smtp.qq.com", 465)

		# 登錄郵箱
		server.login(my_mail, my_pass)

		# 發送郵件
		server.sendmail(my_mail, [mailaddr], msg.as_string())


		# 關閉smtp鏈接
		server.quit()

		redis_client = get_redis_connection('default')  # 指定設置 redis 庫
		redis_client.setex(my_mail, 60 * 5, content)  # 通過鍵 和有效時間 存儲驗證碼
		return Response({'msg':'ok','code':'發送成功',}) 

setting 配置 redis 數據庫

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

 獲取redis 驗證碼

from django_redis import get_redis_connection
redis_client = get_redis_connection('default')  # 指定數據庫
redis_code=redis_client.get(email)  # phone:code  # 獲取數據
if redis_code:
	redis_code= redis_code.decode()
if not code ==redis_code:
	return Response({'msg':'驗證碼不正確'})

  

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM