首先是: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,
['[email protected]']
)
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邮箱会给你一份提示你如何设置
的邮件,你按它里面的提示操作就可以了!