我对django和smtp有些问题。有我的代码:
settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '**********@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
form.py
from django.shortcuts import render
from django import forms
from django.conf import settings
from django.core.mail import send_mail
class ContactForm(forms.Form):
full_name = forms.CharField(required = False)
email = forms.EmailField()
message = forms.CharField()
def contact_admin(request):
# check if form was posted
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = ContactForm(request.POST or None)
# check whether user data is valid:
if form.is_valid():
form_email = form.cleaned_data.get("email")
form_message = form.cleaned_data.get("message")
form_full_name = form.cleaned_data.get("full_name")
# send email
subject = 'Site contact form'
from_email = settings.EMAIL_HOST_USER
to_email = [from_email, 'malfar@ukr.net']
contact_message = "%s: %s via %s" %(form_full_name, form_message, form_email)
try:
send_mail(subject, contact_message, from_email, to_email, fail_silently=True)
except Exception:
message = 'Error'
else:
message = 'Send!'
# if there was not POST render blank form
else:
form = ContactForm()
return render(request, 'contact_admin/form_admin.html', {'form': form})
form.html
<form action='{% url "contact_admin" %}' method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Надіслати" name="send_button" />
</form>
问题是 - 当我尝试发送邮件时,页面开始下载并且需要很长时间,我必须停止它。没有错误或类似的东西。我假设settings.py中的问题,但我不知道如何解决它
我使用django 1.8.3 我的操作系统 - Linux(Ubuntu 14.04)
抱歉我的英语不好))