当我运行服务器时,我的邮件发送没有问题但是当我在views.py中添加最后两行时,电子邮件不再被发送。 /newsite/mail/views.py
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
def send(request):
subject = request.POST.get('subject', 'subject')
message = request.POST.get('message', 'attention ! la temperature a depasse le maximum ')
from_email = request.POST.get('from_email', 'xxxxxxxxxxx@gmail.com')
if subject and message and from_email:
try:
send_mail(subject, message, from_email, ['yyyyyyyyyyyy@gmail.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return HttpResponseRedirect('send_mail')
else:
# In reality we'd use a form class
# to get proper validation errors.
return HttpResponse('Make sure all fields are entered and valid.')
def index(request):
return render(request, 'mail/index.html', {})
/newsite/mail/urls.py
from django.conf.urls import *
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.send, name='send'),
]
/newsite/newsite/settings.py
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=587
EMAIL_HOST_USER='xxxxxxxx@gmail.com'
EMAIL_HOST_PASSWORD='xxxxxxxxx'
EMAIL_USE_TLS = True
答案 0 :(得分:4)
问题出在urlpatterns
。您对index
和send
使用相同的正则表达式/网址。更改send
。
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^send-mail/$', views.send, name='send'),
]
现在,当您访问/send-mail/