Django中内置了邮件发送功能,被定义在django.core.mail模块中发送邮件须要使用SMTP服务器,html
经常使用的免费服务器有:163、126、QQ,下面以163邮件为例子:django
注册163邮箱,服务器
登陆后设置---》POP3/SMTP/IMAP 中打开开发者模式 客户端的受权码‘测试
的在项目的settings.py中加上spa
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.163.com' EMAIL_PORT = 25 #发送邮件的邮箱 EMAIL_HOST_USER = '发送者的邮箱@163.com' #在邮箱中设置的客户端受权密码 EMAIL_HOST_PASSWORD = '发送者的密码' #收件人看到的发件人 EMAIL_FROM = '文字显示<发送者密码@163.com>'
而后在views.py里须要code
#coding:utf-8 from django.shortcuts import render, redirect from django.http import JsonResponse,HttpResponse from models import * from django.conf import settings from django.core.mail import send_mail
# 发送邮件
def send(request):
msg='<a href="http://www.baidu.com" target="_blank">点击激活</a>'
send_mail('测试邮件',
'',
settings.EMAIL_FROM,
['收件箱'],
html_message=msg)
return HttpResponse('ok')htm
最后给配置下路由就好了, 这里就不描述了。。blog