跟随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文件如下

  1.  
    <html>
  2.  
    <head>
  3.  
    <title>Contact us</title>
  4.  
    </head>
  5.  
    <body>
  6.  
    <h1>Contact us</h1>
  7.  
     
  8.  
    {% if errors %}
  9.  
    <ul>
  10.  
    {% for error in errors %}
  11.  
    <li>{{ error }}</li>
  12.  
    {% endfor %}
  13.  
    </ul>
  14.  
    {% endif %}
  15.  
     
  16.  
    <form action="/contact/" method="post">{% csrf_token %}
  17.  
    <p>Subject: <input type="text" name="subject"></p>
  18.  
    <p>Your e-mail (optional): <input type="text" name="email"></p>
  19.  
    <p>Message: <textarea name="message" rows="10" cols="50"></textarea></p>
  20.  
    <input type="submit" value="Submit">
  21.  
    </form>
  22.  
    </body>
  23.  
    </html>

contact/views.py文件如下:

  1.  
    from django.core.mail import send_mail
  2.  
    from django.http import HttpResponseRedirect
  3.  
    from django.shortcuts import render
  4.  
    from django.template import RequestContext
  5.  
    def contact(request):
  6.  
    errors = []
  7.  
    if request.method == 'POST':
  8.  
    if not request.POST.get('subject', ''):
  9.  
    errors.append('Enter a subject.')
  10.  
    if not request.POST.get('message', ''):
  11.  
    errors.append('Enter a message.')
  12.  
    if request.POST.get('email') and '@' not in request.POST['email']:
  13.  
    errors.append('Enter a valid e-mail address.')
  14.  
    if not errors:
  15.  
    send_mail(
  16.  
    request.POST['subject'],
  17.  
    request.POST['message'],
  18.  
    request.POST.get('email', 'noreply@example.com'),
  19.  
    ['接收信件的邮箱!'],
  20.  
    )
  21.  
    return HttpResponseRedirect('/contact/thanks/')
  22.  
    return render(request, 'contact_form.html',
  23.  
    {'errors': errors},context_instance=RequestContext(request))

mysite/mysite/urls.py如下:

  1.  
    from django.conf.urls import *
  2.  
    from django.contrib import admin
  3.  
    from books import views
  4.  
    from mysite.views import emailsuccess
  5.  
    from contact.views import contact
  6.  
    admin.autodiscover()
  7.  
     
  8.  
    urlpatterns = patterns('',
  9.  
    (r'^contact/$',contact),
  10.  
    (r'^contact/thanks/$',emailsuccess),
  11.  
    )

mysite/mysite/views.py文件如下:

  1.  
    from django.http import HttpResponse,Http404
  2.  
    def emailsuccess(request):
  3.  
    return HttpResponse("Send Succeed!")

欢迎交流讨论,共同学习!

转载请注明出处:http://blog.csdn.net/monkeyduck

Django 发送email配置详解及各种错误类型的更多相关文章

  1. redmine邮件发送功能配置详解

    redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...

  2. 【转】Django+Mysql安装配置详解(Linux)

    参考:http://dmyz.org/archives/110 报错TemplateDoesNotExist at 解决: 新建mysite/articles/article.html文件: 文件内容 ...

  3. commons-logging和Log4j 日志管理/log4j.properties配置详解

    commons-logging和Log4j 日志管理 (zz) 什么要用日志(Log)? 这个……就不必说了吧. 为什么不用System.out.println()? 功能太弱:不易于控制.如果暂时不 ...

  4. Maven使用笔记(四)pom.xml配置详解

    pom.xml文件配置详解 --声明规范 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  5. Linux下Tomcat catalina.out自动归档,以及logrotate 配置详解

    Linux下Tomcat catalina.out自动归档 如果 catalina.out 日志达到 2GB 大小的时候,Tomcat 因为缓存问题,便没有办法继续输出日志了.  为了避免这种情况,你 ...

  6. 【转】Maven pom.xml 配置详解

    原文链接:https://yq.aliyun.com/articles/38271 pom.xml文件配置详解 --声明规范 <project xmlns="http://maven. ...

  7. Spark log4j日志配置详解(转载)

    一.spark job日志介绍    spark中提供了log4j的方式记录日志.可以在$SPARK_HOME/conf/下,将 log4j.properties.template 文件copy为 l ...

  8. log4j.properties配置详解与实例

    log4j.properties配置详解与实例 第一步:加入log4j-1.x.x.jar到lib下. 第二步:在工程的src下下建立log4j.properties.内容如下: #OFF,syste ...

  9. keepalived的配置详解(非常详细)

    keepalived的配置详解(非常详细) 2017-01-22 15:24 2997人阅读 评论(0) 收藏 举报  分类: 运维学习(25)    转载自:http://blog.csdn.net ...

随机推荐

  1. js嵌套Struts2标签

    在页面中如果想要在js代码块里面获取到某些值,而这些值是通过Struts的标签取到的, 如: var operatorType = '<s:property value="#sessi ...

  2. python学习(十二)模块

    怎么一下子就来学了模块? 其实学了判断.循环.函数等知识就可以开始下水写程序了,不用在意其他的细节,等你用到的时候再回过头去看,此所谓囫囵吞枣学习法. 为啥学模块? 有点用的.或者有点规模的程序都是要 ...

  3. C# Array类的浅复制Clone()与Copy()的差别

    1 Array.Clone方法 命名空间:System 程序集:mscorlib 语法: public Object Clone() Array的浅表副本仅复制Array的元素,不管他们是引用类型还是 ...

  4. 新西兰天维网登录发送明文password

    新西兰比較有人气的华人社区站点是天维网(新西兰天维网),是这边华人用中文吐槽常常上的论坛,也是华人之间各种交易(比方买卖二手车)的集散地.上次非诚勿扰新西兰专场就是天维网承办的宣传和报名.来新西兰定居 ...

  5. Error -27728: Step download timeout (120 seconds)的解决方法(转)

    LR中超时问题解决方法 超时错误在LoadRunner录制Web协议脚本回放时超时经常出现. 现象1:Action.c(16): Error -27728: Step download timeout ...

  6. springmvc的过滤器和拦截器

    1 什么是过滤器 过滤器是过滤数据,比如过滤低俗文字,修改字符编码等. 2 什么是拦截器 拦截器中可以用来向ModelAndView中添加通用的数据.这样的好处是对于所有网页的公用部分就不需要在每个c ...

  7. 创建node.js一个简单的应用实例

    在node.exe所在目录下,创建一个叫 server.js 的文件,并写入以下代码: //使用 require 指令来载入 http 模块 var http = require("http ...

  8. 【题解】[SCOI2010]股票交易

    十分普通的DP+不平凡的转移 传送门 这道题状态十分明显.转移是\(O(n^4)\)的,过不去,我们需要优化. 一个十分显然的DP是\(f(i,j)\)表示第\(i\)天时候拥有\(j\)单位股票的最 ...

  9. (Android)react-native-splash-screen实践-解决react-native打包好后启动白屏的问题

    1.安装 npm i react-native-splash-screen --save or yarn add react-native-splash-screen --save 2.自动配置 re ...

  10. FI模块与SD、MM的接口配置方法

    [转自 http://blog.itpub.net/195776/viewspace-1023910/] 1 FI/SD 借口配置FI/SD通过tcode VKOA为billing设置过帐科目,用户可 ...