My mail was sent without problem when I runserver but when I added the last two lines in views.py the e-mail can't be sent anymore . /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
The problem is in your urlpatterns
. Your are using the same regex/url for index
and send
. Change the url of send
.
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^send-mail/$', views.send, name='send'),
]
Now the mail should be send when you access /send-mail/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.