python - 我的邮件不能再发送了?

标签 python django django-urls

当我运行服务器时,我的邮件发送没有问题,但是当我在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', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e16161616161616161616162e09030f0702400d0103" rel="noreferrer noopener nofollow">[email protected]</a>')
    if subject and message and from_email:
        try:
            send_mail(subject, message, from_email, ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7cecececececececececececef7d0dad6dedb99d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>'])

        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='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8df5f5f5f5f5f5f5f5cdeae0ece4e1a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>'
EMAIL_HOST_PASSWORD='xxxxxxxxx'
EMAIL_USE_TLS = True

最佳答案

问题出在你的urlpatterns上。您对 indexsend 使用相同的正则表达式/url。更改发送的url。

urlpatterns = [
 url(r'^$', views.index, name='index'),
 url(r'^send-mail/$', views.send, name='send'),
]

现在,当您访问 /send-mail/ 时,应该会发送邮件

关于python - 我的邮件不能再发送了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36008269/

相关文章:

python混淆: dict. pop

django - 如何在 Heroku 上为 Django 应用程序设置数据库?

python - Django,截断不正确的 DOUBLE 值 : '\x00' in ValueQuerySet

Django:urls.py 中的语法错误

python - OpenCV(python)基础矩阵和基本矩阵不一致

Python从两组点中获取变换矩阵

python - 错误 : ' No module named ' django. core.urlresolvers'

python - 在 Django Url Dispatcher 中获取 http 请求数据?这可能吗?如果是这样,如何?

python - 在 python 中编程时钟时,如何允许用户输入自己的当前时间值?

python - 根据旧字典创建新字典