1

一切正常,我收到了我在发送电子邮件时设置的成功消息。但它只是不向给定地址发送电子邮件。当我提交表单时,我还在终端中收到以下消息,本地服务器正在运行。我相信200消息意味着通信成功。

“发布/提交/HTTP/1.1”200 10141

下面给出了我的views.py、settings.py、forms.py 和模板文件。

视图.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from .forms import SubmitForm
from django.template import Context, loader

from django.core.mail import send_mail,EmailMessage



def submit(request):
  success = False
  success_message = " Thanks!"


  form = SubmitForm(request.POST or None)

    if form.is_valid():
        original_title = request.POST.get('original_title')
        english_title = request.POST.get('english_title')

        sent_email = 'receive@email.com'
        message_template = loader.get_template('submit.txt')
        message_context = Context({      'original_title':original_title,'english_title':english_title,
                                    })
        mail = EmailMessage(original_title, english_title, [sent_email])
        mail.send()
        success = True


    return render_to_response('submit.html',
                        {'form' : form,'success': success,'success_message':success_message},
                          context_instance=RequestContext(request))

设置.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'sender@gmail.com'
EMAIL_HOST_PASSWORD = '*****'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = 'Sample < example@gmail.com >

表格.py

from django import forms
from django.conf import settings
from django.core.cache import cache
from django.contrib.admin import widgets    
from django.shortcuts import get_object_or_404
import os


class SubmitForm(forms.Form):
  title = forms.CharField()
  name = forms.CharField()

提交.html

                <form method="post" action="{% url 'submit' %}" style="width:75%;margin:auto">{%csrf_token%}

                <legend>Title</legend>
                    <br/>

                    <div class="row">

                        <dl class="field">
                            <dd><label>Title: *</label></dd>

                            <dt class="text">{{ form.name.errors }}
                                             {{ form.original_title }}</dt>

                            <dd class="msg">You filled this out wrong.</dd>
                        </dl>

                        <dl class="field ">
                            <dd><label>English Title: *</label></dd>

                            <dt class="text">{{ form.name.errors }}
                                                {{ form.english_title }}</dt>

                            <dd class="msg">You filled this out wrong.</dd>
                        </dl>


                        <input class="submit three columns" type="submit" value="Register"/>

我也以适当的方式添加了 submit.txt。当我单击提交但未发送电子邮件时,我收到了 success_message。

4

1 回答 1

0

初始化 时EmailMessage,第三个参数是发件人地址。您似乎有一个包含收件人地址的列表。

确保以与文档相同的方式创建消息:

email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
            ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
            headers = {'Reply-To': 'another@example.com'})
于 2013-06-19T20:15:39.383 回答