跟随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. x86 的 TSS 任务切换机制

    转自:http://blog.chinaunix.net/uid-587665-id-2732907.html [0]写在前面 segment descriptors 构建保护模式下的最基本.最根本的 ...

  2. 最新番茄花园win7系统快速稳定版

    这是最新番茄花园win7系统64位快速稳定版 V2016年2月,该系统由系统妈整理和上传,系统具有更安全.更稳定.更人性化等特点.集成最常用的装机软件,集成最全面的硬件驱动,精心挑选的系统维护工具,加 ...

  3. lumen url重写

    打开nginx配置文件vhosts.conf,加上try_files $uri $uri/ /index.php?$query_string; ,如下 location / { index index ...

  4. IE浏览器的判断

    function compatibleIE8(){ var browser = navigator.appName; var b_version = navigator.appVersion; if( ...

  5. special points about git

    1 about "origin/master tracks the remote branch" 1.1 what does tracking mean? after " ...

  6. cmake默认变量

    1 CMAKE_GENERATOR 用来生成工程构建文件的工具的名字,比如visual studio 12,2013,比如xcode,不同的平台使用不同的生成工具. 2 MATCHES if (var ...

  7. sed相关

    1 global flag sed 's/xxx/xxx/' inputfile,如果没有带global flag g的话,匹配替换的只是inputfile中的每一行的第一个匹配项.如果带了g的话,才 ...

  8. PermissionError: [Errno 13] Permission denied:

    在ubuntu系统下使用pip 命令安装包时,出现以下类似错误提示: PermissionError: [Errno 13] Permission denied: '/usr/local/lib/py ...

  9. Apache NiFi 开发 处理器使用说明

    NIFI的使用: 注意:FlowFile由[属性]和[内容]组成,在解析的过程中这个概念非常重要,因为有些组件操作的是属性,有些组件操作的是内容,在配置组件时Destination配置项的选择很重要, ...

  10. linux 常用shell脚本语句

    最近老大让写一个shell脚本,每天从一个固定IP中取到相应文件,所以想写一个简单的shell脚本命令,供大家学习交流.先做一个简单的例子,先看效果吧, 代码如下: #!/bin/sh #定义一个变量 ...