from django.core.mail import send_mail, BadHeaderError from django.http import HttpResponseRedirect def send_email(request): subject = request.POST.get('subject', 'this is subject') #发送的邮件主题 message = request.POST.get('message', 'it \'s time to go home') #发送的消息 from_email = request.POST.get('from_email', '******') #发件人邮箱 to_email = request.POST.get('to_email', '××××××') #收件人的邮箱 if subject and message and from_email: try: send_mail(subject, message, from_email, [to_email]) #最后一个参数是收件人列表,可发送至多人 except BadHeaderError: return HttpResponse('Invalid header found.') return HttpResponseRedirect('/buy/') else: # In reality we'd use a form class # to get proper validation errors. return HttpResponse('Make sure all fields are entered and valid.') #//python/5818