1

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

我的配置如下,类似于Patrick Beeson 的

settings.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  

网址.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 }}

已编辑

action="."在我的contact_form.html中设置并添加时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']

我自己的新views.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.', 'from@example.com', ['to@example.com'], fail_silently=False)
        print request.POST
    return render_to_response(template_name, { 'form': form }, context_instance=RequestContext(request))

这里出了什么问题?

4

2 回答 2

0

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

编辑

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

设置.py:

from local import localsettings

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Jasper Kennis', 'jasper.kennis@companymail.nl'),
)

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 %} 。在这种情况下,您必须自己构建表单模板。让我知道这是否有帮助。

于 2011-01-06T12:54:28.867 回答
0

确保 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]

再次确保在 yout settings.py 中设置电子邮件端口主机设置

EMAIL_HOST = 'yourmailserver'
EMAIL_PORT = "587"
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = True
于 2012-03-07T19:08:04.353 回答