django邮件

 1、邮件变量

(django settings.py)

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   #错误消息来自的电子邮件地址

2、实例设置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设置:

from blogZh.views import email

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

email.html设置:

<!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>

 

posted @ 2018-08-25 11:27  Py.qi  阅读(623)  评论(0编辑  收藏  举报