1.在setting.py中添加配置邮箱信息:
EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’
EMAIL_HOST = ‘smtp.163.com’ #服务器
EMAIL_PORT = 25 #端口,一般情况下都为25
EMAIL_HOST_USER = ‘19941203892@163.com’ #账号
EMAIL_HOST_PASSWORD = ‘19951027abcd’ #授权码
DEFAULT_FROM_EMAIL = ‘花生二手车19941203892@163.com’ #邮箱来自
2.在views.py中导入:
from django.core.mail import send_mail #导入发送邮件
import guazi.settings #导入smtp
from itsdangerous import TimedJSONWebSignatureSerializer as TJS#导入itsdangerous包
3.继续配置views文件:
ts = TJS(guazi.settings.SECRET_KEY)
trans = ts.dumps(user_name)
trans = trans.decode()
print(trans)
subject = '这是瓜子二手车项目'
html_message = "<a href = 'http://127.0.0.1:8000/user/active/"+trans+"'>这是激活邮件</a> "
send_mail(subject=subject, message='', from_email='19941203892@163.com', recipient_list=[e_mail], html_message=html_message)
return HttpResponse('ok')
注:先在cmd中安装itsdangerous: pip install itsdangerous