settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
# 'formatters': {
# 'simple': { # exact format is not important, this is the minimum information
# 'format': '%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
# },
# },
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
'mail_admins': {
# Add Handler for mail_admins for `warning` and above
# 'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
},
'file': {
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'admin.log'),
},
},
'root': {
'handlers': ['console', 'file','mail_admins'],
'level': 'INFO',
},
}
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# 我使用的是新浪的,host可以在對應郵箱的設置中找到
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 25
# 你的郵箱賬號與密碼
EMAIL_HOST_USER = 'ci_s******@163.com'
EMAIL_HOST_PASSWORD = 'M******'
# 由於使用25端口,一般都不使用TLS機密,SSL和TSL只需要設置一個,他們同時為True或False
EMAIL_USE_TLS = False
# 發件人,只有這個變量名可以自己自定義,設置在這裡是為了減少每次去寫
EMAIL_FROM = 'ci_******@163.com'
views.py
if user:
if user.is_active:
login(request,user)
logger.info("%s %s login" % (datetime.now(),username))
send_mail('Subject',
'hi',
'ci_*******@163.com',
['S*******@wistron.com'],
fail_silently=False,
)
print('send_mail')
return HttpResponseRedirect('/')
else:
pass
else:
.......