def contactform(request):
if request.method == 'GET':
form = contact_form()
else:
form = contact_form(request.POST)
if form.is_valid():
contact_name = form.cleaned_data['aname']
contact_email = form.cleaned_data['aemail']
contact_subject = form.cleaned_data['asubject']
contact_message = form.cleaned_data['rmessage']
try:
email_from = settings.EMAIL_HOST_USER
recipient_list = ['xxx@gmail.com']
html_message = render_to_string('main/contact.html', {'contact_email': contact_email, 'contact_name': contact_name, 'contact_message': contact_message})
msg = EmailMessage(contact_subject, html_message, email_from, recipient_list)
msg.content_subtype = "html"
msg.send()
except ValueError:
return HttpResponse('Invalid header found.')
return render(request, "main/home.html", {'successcform': 'Success! Thank you for your message. We will reply shortly.', 'contactform': form})
return render(request, "main/home.html", {'contactform': form})
这很好用,当有人填写联系表时发送电子邮件,但如果我使用发送邮件代码:
def signupuser(request):
if request.method == 'GET':
return render(request, 'main/signupuser.html', {'form':UserCreationForm()})
else:
if request.POST['password1'] == request.POST['password2']:
try:
user = User.objects.create_user(request.POST['username'], password=request.POST['password1'])
user.save()
login(request, user)
email_from = settings.EMAIL_HOST_USER
recipient_list = [request.POST['username']]
contact_subject = [' ,thank you for registering to our site.']
html_message = render_to_string('main/signupuser.html', {'username': request.POST['username']})
msg = EmailMessage(contact_subject, html_message, email_from, recipient_list)
msg.content_subtype = "html"
msg.send()
return redirect('currenttodos')
except IntegrityError:
return render(request, 'todo/signupuser.html', {'form':UserCreationForm(), 'error':'That username has already been taken. Please choose a new username'})
else:
return render(request, 'todo/signupuser.html', {'form':UserCreationForm(), 'error':'Passwords did not match'})
邮件不会发送,用户注册时收不到任何邮件。当我这样使用它时根本不起作用。控制台中没有错误。