跟随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. spring-web中的WebDataBinder理解

    Spring可以自动封装Bean,也就是说前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等).如果传递过来的是 ...

  2. 做一个合格的程序员之浅析Spring AOP源代码(十八) Spring AOP开发大作战源代码解析

    事实上上一篇文章价值非常小,也有反复造轮子的嫌疑,网上AOP的实例非常多,不胜枚举,事实上我要说的并非这个,我想要说的就是上一节中spring的配置文件: 我们这边并没实用到我们上几节分析的哪几个AO ...

  3. win7激活附带激活软件

    链接: https://pan.baidu.com/s/1i46yoHR 密码: 7k6y

  4. 【BZOJ4861】[Beijing2017]魔法咒语 矩阵乘法+AC自动机+DP

    [BZOJ4861][Beijing2017]魔法咒语 题意:别看BZ的题面了,去看LOJ的题面吧~ 题解:显然,数据范围明显的分成了两部分:一个是L很小,每个基本词汇长度未知:一个是L很大,每个基本 ...

  5. protobuf json xml比较

    1 protobuf/xml/json对比 从数据的存储格式的角度进行对比 假如要存储一个键值对: {price:150} 1.1 protobuf的表示方式 message  Test { opti ...

  6. 【题解】CJOI2019 登峰造鸡境 (Prufer序列+斯特林数)

    [题解]CJOI2019 登峰造鸡境 (Prufer序列+斯特林数) 题目背景 舒服了. 题目描述 你有一颗n个点的无根树,每个点有有一个标号(1~n). 现在你知道,总共有m个叶子节点,求不同的树的 ...

  7. 【题解】P1156垃圾陷阱

    [题解]P1156 垃圾陷阱 乍看此题,我们感觉状态很多,很复杂. 遇到这类型条件比较多的\(dp\),我们不要首先考虑全部设出来,而是要看到这些状态的本质.而在这道题目中,时间和高度就是关键. 考虑 ...

  8. Java语言基础(回头复习)

    /* 使用变量的时候要注意的问题: A:作用域 变量定义在哪个大括号内,它就在这个大括号内有效. 并且,在同一个大括号内不能同时定义同名的变量. B:初始化值 没有初始化值的变量不能直接使用. 你只要 ...

  9. 序列化组件(get/put/delete接口设计),视图优化组件

    一 . 知识点回顾 1 . 混入类 , 多继承 class Animal(object): def eat(self): print("Eat") def walk(self): ...

  10. String源码中hashCode算法

    针对java中String源码hashcode算法源码分析 /** The value is used for character storage. */ private final char val ...