邮件不是由 django-contact-form 应用程序发送的,并且没有错误消息

发布于 2024-10-10 15:14:29 字数 3689 浏览 5 评论 0原文

我已经安装了这个 django-contact-form 应用程序。 当我想通过联系表单向我的 Gmail 帐户发送电子邮件时,我没有收到任何错误,并且我被重定向到我的 contact_form_sent.html,但没有发送任何邮件。谁能帮我调试这个问题。这是 smtp 服务器的问题吗,因为我是从本地主机测试的???

我的配置如下,与Patrick Beeson的类似:

设置.py 我已经检查过其他电子邮件帐户及其适当的端口。

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
      #('***', '***@googlemail.com'),
)

MANAGERS = ADMINS

EMAIL_HOST = 'smtp.gmail.com'   
EMAIL_HOST_PASSWORD = '********'  
EMAIL_HOST_USER = '******@googlemail.com' 
EMAIL_PORT = 587 #465 or 587  
#EMAIL_SUBJECT_PREFIX = 'Django Test mail'  
EMAIL_USE_TLS = True  

urls.py

(r'^contact/', include('contact_form.urls')),

contact_form.html

...
              <form method="POST">
              <ol>
              {{ form.as_p }}
              <li>
          <input type="submit" name="submit" value={% trans "Senden" %} />
                <div class="clr"></div>
              </li>
              </ol>
              </form>
...

{{ name }}
{{ email }}
{{ body }}

contact_form.txt contact_form_sent.html

{% block content %}
           <h2>{% trans "Your message was sent." %}</h2>
{% endblock %}

contact_form_subject.txt

message from {{ name }}

在我的 contact_form.html 中设置 action="." 并添加 print request.POST 时进行编辑

,我在本地主机的开发服务器中得到了这个,单击提交按钮后:

<QueryDict: {u'body': [u'This is my Test message for you !!\r\n\r\nBest Regards\r\nMr. NoOne'], u'name': [u'testname'], u'submit': [u'Send'], u'email': [u'anyone@myemail.
com']}>

已编辑

如果我编写自己的 view.py,我会收到此错误:

Request Method:     POST
Request URL:    http://127.0.0.1:8000/en/contact/
Exception Type:     error
Exception Value:    

(10065, 'No route to host')

Exception Location:     C:\Python25\lib\smtplib.py in connect, line 310
Python Executable:  C:\Python25\python.exe
Python Version:     2.5.0
Python Path:    ['H:\\webpage', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages']

我新的自己的 view.py 看起来像:

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.mail import send_mail

from contact_form.forms import ContactForm


def email_contact(request, form_class=ContactForm, template_name='contact_form/contact_form.html'):
    form = form_class(data=request.POST, files=request.FILES, request=request)
    if request.method == "POST":
        send_mail('Subject here', 'Here is the message.', '[email protected]', ['[email protected]'], fail_silently=False)
        print request.POST
    return render_to_response(template_name, { 'form': form }, context_instance=RequestContext(request))

这里出了什么问题?

I've installed this django-contact-form app.
When I want to send an email to my gmail account via contact form I get no error and I am redirected to my contact_form_sent.html, but there is no mail sent. Can anyone help me in debugging this issue. Is this a problem of smtp server, because I am testing from localhost???

My configurations are as follows, it is similar to Patrick Beeson's :

settings.py
I have already checked other email accounts and their appropiate ports.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
      #('***', '***@googlemail.com'),
)

MANAGERS = ADMINS

EMAIL_HOST = 'smtp.gmail.com'   
EMAIL_HOST_PASSWORD = '********'  
EMAIL_HOST_USER = '******@googlemail.com' 
EMAIL_PORT = 587 #465 or 587  
#EMAIL_SUBJECT_PREFIX = 'Django Test mail'  
EMAIL_USE_TLS = True  

urls.py

(r'^contact/', include('contact_form.urls')),

contact_form.html

...
              <form method="POST">
              <ol>
              {{ form.as_p }}
              <li>
          <input type="submit" name="submit" value={% trans "Senden" %} />
                <div class="clr"></div>
              </li>
              </ol>
              </form>
...

contact_form.txt

{{ name }}
{{ email }}
{{ body }}

contact_form_sent.html

{% block content %}
           <h2>{% trans "Your message was sent." %}</h2>
{% endblock %}

contact_form_subject.txt

message from {{ name }}

EDITED

when setting action="." in my contact_form.html and adding print request.POST I get this in my development server at localhost, after clicking the submit button:

<QueryDict: {u'body': [u'This is my Test message for you !!\r\n\r\nBest Regards\r\nMr. NoOne'], u'name': [u'testname'], u'submit': [u'Send'], u'email': [u'anyone@myemail.
com']}>

EDITED

If I write my own view.py I get this error:

Request Method:     POST
Request URL:    http://127.0.0.1:8000/en/contact/
Exception Type:     error
Exception Value:    

(10065, 'No route to host')

Exception Location:     C:\Python25\lib\smtplib.py in connect, line 310
Python Executable:  C:\Python25\python.exe
Python Version:     2.5.0
Python Path:    ['H:\\webpage', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages']

my new own views.py looks like:

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.mail import send_mail

from contact_form.forms import ContactForm


def email_contact(request, form_class=ContactForm, template_name='contact_form/contact_form.html'):
    form = form_class(data=request.POST, files=request.FILES, request=request)
    if request.method == "POST":
        send_mail('Subject here', 'Here is the message.', '[email protected]', ['[email protected]'], fail_silently=False)
        print request.POST
    return render_to_response(template_name, { 'form': form }, context_instance=RequestContext(request))

What is going wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-10-17 15:14:29

您确定表单已提交吗?因为如果您提交一个带有 debug true 的表单,并且表单中不存在 csrf 令牌,Django 应该会给出错误。另一个想法;您正在与 smtp.gmail.com 通话,但使用 @googlemail.com 地址。你确定能应付吗?

编辑

我做了不同的事情;我没有使用现有的应用程序,而是构建了一个新的应用程序。我的应用程序需要做的不仅仅是邮寄。而且它只有两个文件,所以应该有帮助。我也使用过 gmail,另一个优点:) 我会给你相关的代码,但是请注意;)

settings.py:

from local import localsettings

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Jasper Kennis', '[email protected]'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': localsettings.db_engine,
        'NAME': localsettings.db_name,
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Make this unique, and don't share it with anybody.
SECRET_KEY = '###'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
        'therelevantapp',
)


EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '###@gmail.com'
EMAIL_HOST_PASSWORD = '###'
EMAIL_PORT = 587

和应用程序,app/views.py

import os
import os.path
import string
from random import choice

from app.models import *
from TitelGenerator.TitelGenerator import TitelGenerator
from django import forms
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives



def klaar(request):
    if request.method == "POST":
        title = 'Je inschrijving is voltooid!'
        message = 'a whole message'
        send_mail( title , message , 'sendermail' , 'receivermail' ,     fail_silently=False)

        return render_to_response('klaar.html' , { 'titel': kind.id, 'request':     request.FILES }, context_instance = RequestContext(request))

请注意,你真的需要有 {% csrf_token %} 在您的表单中。在这种情况下,您必须自己构建表单模板。让我知道这是否有帮助。

Are you sure the form even submits? Because if you submit a form with debug true, and no csrf token present in your form, Django should give an error. Another idea; you're talking to smtp.gmail.com, but use an @googlemail.com address. Are you sure that copes?

EDIT

I did this different; instead of using an existing app I build a new one. My app needed to do more than just mailing. And it's only two files, so that should help. I used gmail too, another plus:) I'll give you the relevant code, but cut peaces out, so watch that;)

settings.py:

from local import localsettings

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Jasper Kennis', '[email protected]'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': localsettings.db_engine,
        'NAME': localsettings.db_name,
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Make this unique, and don't share it with anybody.
SECRET_KEY = '###'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
        'therelevantapp',
)


EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '###@gmail.com'
EMAIL_HOST_PASSWORD = '###'
EMAIL_PORT = 587

and the app, app/views.py

import os
import os.path
import string
from random import choice

from app.models import *
from TitelGenerator.TitelGenerator import TitelGenerator
from django import forms
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives



def klaar(request):
    if request.method == "POST":
        title = 'Je inschrijving is voltooid!'
        message = 'a whole message'
        send_mail( title , message , 'sendermail' , 'receivermail' ,     fail_silently=False)

        return render_to_response('klaar.html' , { 'titel': kind.id, 'request':     request.FILES }, context_instance = RequestContext(request))

Note that you REALLY NEED to have {% csrf_token %} inside your form. The form template you'll have to build yourself in this case. Let me know if this helped.

樱&纷飞 2024-10-17 15:14:29

确保 settings.py 中的 ADMIN、MANAGERS 设置可用,因为它会

再次在 contact_form 的 forms.py 中

class BaseEmailFormMixin(object):
    from_email = settings.DEFAULT_FROM_EMAIL
    recipient_list = [email for _, email in settings.MANAGERS]

查找这些电子邮件确保在 settings.py 中设置电子邮件端口主机设置

EMAIL_HOST = 'yourmailserver'
EMAIL_PORT = "587"
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = True

make sure your ADMIN,MANAGERS settings in your settings.py is available because it's lookin for these emails

in forms.py of contact_form

class BaseEmailFormMixin(object):
    from_email = settings.DEFAULT_FROM_EMAIL
    recipient_list = [email for _, email in settings.MANAGERS]

again make sure you set email port host setting in yout settings.py

EMAIL_HOST = 'yourmailserver'
EMAIL_PORT = "587"
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文