django郵件

 一、郵件變量

(django settings.py)html

ADMINS = [('JOHN','JOHN@example.com'),('zhang','zhang@example.com')]   #設置收件人名單
DEFAULT_CHARSET = utf-8   #設置默認郵件字符類型
DEFAULT_FROM_EMAIL =    #設置站點管理員的默認郵件地址
EMAIL_BACKEND    #設置發送電子郵件的後端接口
EMAIL_FILE_PATH    #設置郵件後端輸出文件路徑
EMAIL_HOST    #設置發送電子郵件的主機
EMAIL_HOST_PASSWORD   #設置郵件登錄密碼
EMAIL_HOST_USER   #設置郵件登錄用戶名
EMAIL_PORT    #設置郵件端口
EMAIL_SSL_CERTFILE   #設置指定證書文件路徑
EMAIL_SSL_KEYFILE   #設置置頂證書密鑰路徑
EMAIL_SUBJECT_PREFIX   #設置郵件主題前綴
EMAIL_TIMEOUT   #設置超時時間
EMAIL_USE_LOCALTIME = True   #是否以date本地時區發送郵件SMTP頭
EMAIL_USE_TLS    #設置顯式的TLS安全連接,端口587
EMAIL_USE_SSL   #設置隱式的SSL安全鏈接,端口465,請注意EMAIL_USE_TLS/ EMAIL_USE_SSL是互斥的,所以只將其中一個設置爲True
MANAGERS   #指定BrokenLinkEmailsMiddleware啓用時應獲取斷開連接通知的人員郵件地址
SERVER_EMAIL   #錯誤消息來自的電子郵件地址 django

二、實例設置django郵件報警

settings.py文件配置:後端

#email setting is admin
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = '920664709@163.com'
EMAIL_HOST_PASSWORD = '123'
DEFAULT_FROM_EMAIL = '920664709@163.com'
DEFAULT_CHARSET = 'utf-8'

views.py文件設置:安全

from django.shortcuts import render
from django.core.mail import send_mail

def email(request):
    if request.method == 'POST':
    send_mail(
request.POST['subject'],
request.POST['message'],
request.POST['fromemail'],
[request.POST['toemail']],
)
    return render(request,'email.html')

urls.py設置:post

from blogZh.views import email

urlpatterns = [
path('email/',email,name='email'),
]

email.html設置:url

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>email test</title>
</head>
<body>
<form action="" method="post">
<p>郵件主題:<input type="text" name="subject"/></p>
<p>郵件內容:<textarea cols="21" rows="3" name="message"></textarea>
<p>發件人:<input type="text" name="fromemail"/></p>
<p>收件人:<input type="text" name="toemail"/></p>
<p><input type="submit" value="發送郵件"/></p>
</form>
</body>
</html>
相關文章
相關標籤/搜索