django实现如何向QQ邮箱发送邮件

 

首先在settings中进行设置以下操作:

首先是:EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend',确保你使用的是smtp

接着设置一下操作:

EMAIL_HOST = 'smtp.qq.com'  QQ邮箱的STMP服务器
EMAIL_PORT = 25   QQ邮箱的STMP端口号
EMAIL_HOST_USER='*********'  你的QQ用户名
EMAIL_HOST_PASSWORD='*******' 你的QQ密码


你的contact.py中:

@login_required
def contacts(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            topic = form.cleaned_data['topic']
            message = form.cleaned_data['message']
            sender = form.cleaned_data['sender']
            send_mail(
                'Feedback from your site, topic: %s' % topic,
                message, sender,
                ['772052869@qq.com']
            )
            return HttpResponseRedirect('/contacts/thanks/')
    else:
        form = ContactForm()
    return render_to_response('contact/contact.html', {
                            'form':form
                            }, RequestContext(request)
                            )


@login_required
def thanks(request):
    return render_to_response('contact/thanks.html', {
            }, context_instance=RequestContext(request))


contact.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Contact us</title>
</head>
<body>
    <h1>Contact us</h1>
    <form action="." method="POST">
    {% csrf_token %}
        <table>
            {{ form.as_table }}
        </table>
        <p><input type="submit" value="Submit"></p>
    </form>
</body>
</html>


thanks.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Thanks</title>
 </head>
 <body>
  <h1>Thanks,your email is send out</h1>
 </body>
 </html>


接着在你的contact.html里有form,以及thanks.html有发送消息反馈。

当你填完表单后提交这时你的邮件内容就会被发送到你QQ邮箱上!注意你的QQ必须开启STMP功能。如果你没开启,你继续以上的操作后,QQ邮箱会给你一份提示你如何设置

的邮件,你按它里面的提示操作就可以了!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值