Django 发送email配置详解及各种错误类型
跟随Django Book的内容发送邮件不成功,总结一下需要配置好settings.py文件,还要注意一些细节。
1、在settings文件最后添加以下内容,缺一不可!
EMAIL_HOST= 'smtp.163.com'
EMAIL_PORT= 25
EMAIL_HOST_USER = 'xxxxxx@163.com'(你有163邮箱的话)
EMAIL_HOST_PASSWORD = ‘xxxxxxx'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
这里Host邮箱最好还是写163的,qq的我试过不行,别的也不敢保证可以。如果出现“STARTTLS extension not supported by server”这种错误类型很有可能是邮箱不支持。
2、出现Forbidden (403)“CSRF verification failed. Request aborted.”的错误
需要添加{% csrf_token %}到form中去,<form action="/contact/" method="post">{% csrf_token %}<p>...</p>...</form>
之后在contact()方法的views.py中添加from django.template import RequestContext并且最后一句改为return render_to_response('contact_form.html',{'errors': errors}, context_instance=RequestContext(request))即可
3、我的目录结构是这样的:
mysite-->
books/
contact/
mysite/
templates/
manage.py
4.最后附上完整代码
templates/contact_form.html文件如下
- <html>
- <head>
- <title>Contact us</title>
- </head>
- <body>
- <h1>Contact us</h1>
- {% if errors %}
- <ul>
- {% for error in errors %}
- <li>{{ error }}</li>
- {% endfor %}
- </ul>
- {% endif %}
- <form action="/contact/" method="post">{% csrf_token %}
- <p>Subject: <input type="text" name="subject"></p>
- <p>Your e-mail (optional): <input type="text" name="email"></p>
- <p>Message: <textarea name="message" rows="10" cols="50"></textarea></p>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
contact/views.py文件如下:
- from django.core.mail import send_mail
- from django.http import HttpResponseRedirect
- from django.shortcuts import render
- from django.template import RequestContext
- def contact(request):
- errors = []
- if request.method == 'POST':
- if not request.POST.get('subject', ''):
- errors.append('Enter a subject.')
- if not request.POST.get('message', ''):
- errors.append('Enter a message.')
- if request.POST.get('email') and '@' not in request.POST['email']:
- errors.append('Enter a valid e-mail address.')
- if not errors:
- send_mail(
- request.POST['subject'],
- request.POST['message'],
- request.POST.get('email', 'noreply@example.com'),
- ['接收信件的邮箱!'],
- )
- return HttpResponseRedirect('/contact/thanks/')
- return render(request, 'contact_form.html',
- {'errors': errors},context_instance=RequestContext(request))
mysite/mysite/urls.py如下:
- from django.conf.urls import *
- from django.contrib import admin
- from books import views
- from mysite.views import emailsuccess
- from contact.views import contact
- admin.autodiscover()
- urlpatterns = patterns('',
- (r'^contact/$',contact),
- (r'^contact/thanks/$',emailsuccess),
- )
mysite/mysite/views.py文件如下:
- from django.http import HttpResponse,Http404
- def emailsuccess(request):
- return HttpResponse("Send Succeed!")
欢迎交流讨论,共同学习!
转载请注明出处:http://blog.csdn.net/monkeyduck
Django 发送email配置详解及各种错误类型的更多相关文章
- redmine邮件发送功能配置详解
redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...
- 【转】Django+Mysql安装配置详解(Linux)
参考:http://dmyz.org/archives/110 报错TemplateDoesNotExist at 解决: 新建mysite/articles/article.html文件: 文件内容 ...
- commons-logging和Log4j 日志管理/log4j.properties配置详解
commons-logging和Log4j 日志管理 (zz) 什么要用日志(Log)? 这个……就不必说了吧. 为什么不用System.out.println()? 功能太弱:不易于控制.如果暂时不 ...
- Maven使用笔记(四)pom.xml配置详解
pom.xml文件配置详解 --声明规范 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- Linux下Tomcat catalina.out自动归档,以及logrotate 配置详解
Linux下Tomcat catalina.out自动归档 如果 catalina.out 日志达到 2GB 大小的时候,Tomcat 因为缓存问题,便没有办法继续输出日志了. 为了避免这种情况,你 ...
- 【转】Maven pom.xml 配置详解
原文链接:https://yq.aliyun.com/articles/38271 pom.xml文件配置详解 --声明规范 <project xmlns="http://maven. ...
- Spark log4j日志配置详解(转载)
一.spark job日志介绍 spark中提供了log4j的方式记录日志.可以在$SPARK_HOME/conf/下,将 log4j.properties.template 文件copy为 l ...
- log4j.properties配置详解与实例
log4j.properties配置详解与实例 第一步:加入log4j-1.x.x.jar到lib下. 第二步:在工程的src下下建立log4j.properties.内容如下: #OFF,syste ...
- keepalived的配置详解(非常详细)
keepalived的配置详解(非常详细) 2017-01-22 15:24 2997人阅读 评论(0) 收藏 举报 分类: 运维学习(25) 转载自:http://blog.csdn.net ...
随机推荐
- spring-web中的WebDataBinder理解
Spring可以自动封装Bean,也就是说前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等).如果传递过来的是 ...
- 做一个合格的程序员之浅析Spring AOP源代码(十八) Spring AOP开发大作战源代码解析
事实上上一篇文章价值非常小,也有反复造轮子的嫌疑,网上AOP的实例非常多,不胜枚举,事实上我要说的并非这个,我想要说的就是上一节中spring的配置文件: 我们这边并没实用到我们上几节分析的哪几个AO ...
- win7激活附带激活软件
链接: https://pan.baidu.com/s/1i46yoHR 密码: 7k6y
- 【BZOJ4861】[Beijing2017]魔法咒语 矩阵乘法+AC自动机+DP
[BZOJ4861][Beijing2017]魔法咒语 题意:别看BZ的题面了,去看LOJ的题面吧~ 题解:显然,数据范围明显的分成了两部分:一个是L很小,每个基本词汇长度未知:一个是L很大,每个基本 ...
- protobuf json xml比较
1 protobuf/xml/json对比 从数据的存储格式的角度进行对比 假如要存储一个键值对: {price:150} 1.1 protobuf的表示方式 message Test { opti ...
- 【题解】CJOI2019 登峰造鸡境 (Prufer序列+斯特林数)
[题解]CJOI2019 登峰造鸡境 (Prufer序列+斯特林数) 题目背景 舒服了. 题目描述 你有一颗n个点的无根树,每个点有有一个标号(1~n). 现在你知道,总共有m个叶子节点,求不同的树的 ...
- 【题解】P1156垃圾陷阱
[题解]P1156 垃圾陷阱 乍看此题,我们感觉状态很多,很复杂. 遇到这类型条件比较多的\(dp\),我们不要首先考虑全部设出来,而是要看到这些状态的本质.而在这道题目中,时间和高度就是关键. 考虑 ...
- Java语言基础(回头复习)
/* 使用变量的时候要注意的问题: A:作用域 变量定义在哪个大括号内,它就在这个大括号内有效. 并且,在同一个大括号内不能同时定义同名的变量. B:初始化值 没有初始化值的变量不能直接使用. 你只要 ...
- 序列化组件(get/put/delete接口设计),视图优化组件
一 . 知识点回顾 1 . 混入类 , 多继承 class Animal(object): def eat(self): print("Eat") def walk(self): ...
- String源码中hashCode算法
针对java中String源码hashcode算法源码分析 /** The value is used for character storage. */ private final char val ...