Tested in django1.9
.Transfer http
request to smtp
then send Email.
set var about Email in settings
EMAIL_HOST = 'smtp.qq.com' # EMAIL_HOST_PORT = '' EMAIL_HOST_USER = 'yourname@qq.com' EMAIL_HOST_PASSWORD = 'yourpassword'
|
views.py
from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect from django.core.mail import send_mail,BadHeaderError def index(request): return render(request,'index.html') def send_email(request): from_email='yourname@qq.com' subject = request.POST.get('subject', '') to = request.POST.get('tos', '') message = request.POST.get('content', '') if subject and message and from_email: try: send_mail(subject, message, from_email, to.split(',')) except BadHeaderError: return render(request,'wrong.html') return render(request,'success.html') else: return HttpResponse('Make sure all fields are entered and valid.')
|
index.html
<body> <form action="send/" method='post'> {% csrf_token %} {{ form }} http to smtp <br /> <label>发件人 from</label> <label>修改后不能使用</label> <br/> <input type="email" name="from_email" id="emailFromId" value="yourname@qq.com"> <br/> <label>收件人 to</label> <label>多个收件人请用英文逗号分割</label> <br/> <input type="text" name="tos" id="emailToId" > <br/> <label>主题 subject</label> <input type="text" name="subject" id="subjectId" value="suject here"> <br/> <label>正文 content</label> <input type="test" name="content" id="messageId" value="this is a test message"> <input type="submit" value="提交"> </form> </body>
|
urls.py
from django.conf.urls import url from django.contrib import admin from yourappimport views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.index, name='index'), url(r'^send/$', views.send_email, name='send_email'), ]
|
official documents about django in docs.djangoproject.com
sending Email
csrf