django book 第7章发送邮件碰到的种种问题

来源:互联网 发布:豆荚网络加速器注册 编辑:程序博客网 时间:2024/06/15 19:48

django中文版和英文版:(建议不想看英文版的可以参考中文版文字,但是代码不要看中文版的,有些出入会让你迷惑)。

http://djangobook.py3k.cn/2.0/chapter07/

<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>


from django.core.mail import send_mailfrom django.http import HttpResponseRedirectfrom django.shortcuts import renderdef 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'),                ['siteowner@example.com'],            )            return HttpResponseRedirect('/contact/thanks/')   return render(request, 'contact_form.html', {'errors': errors})   #不要用render_to_response,用render更方便


settings.py设置添加:

EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = '25'
EMAIL_HOST_USER = 'youremail@163.com'
EMAIL_HOST_PASSWORD = 'your passward'
EMAIL_USE_TLS = False


几个需要注意的问题:(django 1.7.1)

1.html里边form需要加上{% csrf_tokon %},关于csrf攻击可以搜索相关信息。


2.views.py中的方法不建议使用render_to_response。在这里有两种调用方式:

return render_to_response('contact_form.html',{'errors':errors},context_instance=RequestContext(request));

return render(request, 'contact_form.html', {'errors':errors});

在英文版的django book用的都是render,render封装了最后的RequestContext(request),用起来更加简洁。详细区别参考链接:

http://stackoverflow.com/questions/5154358/django-what-is-the-difference-between-render-render-to-response-and-direc


3.关于函数send_mail:

https://docs.djangoproject.com/en/dev/topics/email/

send_mail('Subject here', 'Here is the message.', 'from@example.com',  ['to@example.com'], fail_silently=False);

函数原型很容易懂,但是这里有些让人困惑的地方。

 if not errors:            send_mail(                request.POST['subject'],                request.POST['message'],                request.POST.get('email', 'noreply@example.com'),                ['siteowner@example.com'],            )

你在settings.py里边配置的是request.POST.get('email', 'noreply@example.com'),这一句的邮件发送服务器,而不是你的站点管理人员的。也就是说,你这里设置的东西是提供给用户用来发邮件用的,但是如果别人留下了自己的的邮箱名又会出现“'mail from address must be same as authorization user' ”的错误,因为别人的邮箱肯定和你settings.py里边设置的不匹配(不要误解使用了用户填的邮箱发送,使用的是你设置的邮件服务器)。后边的siteowner@example.com是可以随便填的接收邮箱,与设置无关。

所以我想这么解决,你随便申请个163邮箱用来给人发邮件,然后第三个参数就默认填这个邮箱,也就是settings.py里边需要设置的。然后把用户留下的email添加到邮件正文里边给你发过去,最后的siteowner@example.com是你用来接受用户反馈的邮箱。

我看到评注里边有说三四参数颠倒的,应该是没有明白settings.py设置的是给用户提供的一个邮件服务器用的,你要是颠倒了的话就让别人随意使用你的邮箱发送了。


4.邮箱设置问题。

163默认是开通了smtp的,但是qq没有开通,你需要到qq邮箱账户设置里边开通才可以,要不会出现Authentication failed, please open smtp flag first!'或者

Connection refused等错误。

0 0
原创粉丝点击