将Django电子邮件打印到控制台的问题

时间:2018-04-06 16:56:51

标签: python python-3.x django-forms django-views django-2.0

我不确定我的逻辑有什么问题,但是当我提交表单时,它会在浏览器中呈现Httpresponse,但不会将电子邮件发送到控制台。我希望视图功能能够成功打印到控制台。后来我可能会实现sendgrid。我只想在开始潜入之前运行成功的控制台打印!感谢。

控制台输出:

Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[06/Apr/2018 11:15:30] "GET /app01/contact_us/ HTTP/1.1" 200 2880
[06/Apr/2018 11:15:40] "POST /app01/contact_us/ HTTP/1.1" 200 30

settings.py包括:

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

view.py

from django.shortcuts import render
from django.views import generic
from django.core.mail import EmailMessage
from django.template.loader import get_template
from django.contrib import messages

from .models import *
from .forms import ContactForm

# Create your views here.
def contact_form(request):
form_class = ContactForm

if request.method == 'POST':
    form = form_class(data=request.POST)

    if form.is_valid():
        contact_name = request.POST.get('contact_name', '')
        contact_email = request.POST.get('contact_email', '')
        contact_phone = request.POST.get('contact_phone', '')
        move_date = request.POST.get('move_date', '')
        address_from = request.POST.get('address_from', '')
        address_to = request.POST.get('address_to', '')
        contact_access = request.POST.get('contact_access', '')
        additional_information = request.POST.get('additional_information', '')
        contact_hear = request.POST.get('contact_hear', '')

        template = get_template('app01/contact_template.txt')
        context = {
            'contact_name': contact_name,
            'contact_email': contact_email,
            'contact_phone': contact_phone,
            'move_date': move_date,
            'address_from': address_from,
            'address_to': address_to,
            'contact_access': contact_access,
            'additional_information': additional_information,
            'contact_hear': contact_hear,
        }
        content = template.render(context)

        email = EmailMessage(
            'New Estimate Request',
            content,
            to=['myemailaddress@gmail.com'],
            headers = {'Reply-To': contact_email},
        )
        email.send()
        messages.success(request, 'Email successfully submitted.')
        return render(request, 'app01/contact_us.html', {'form': form_class, })

return render(request, 'app01/contact_us.html', {'form': form_class, })

forms.py

from django import forms

ACCESS_CHOICES = (
    ('1', 'No'),
    ('2', 'Yes')
)
HEAR_CHOICES = (
    ('1', 'Search Engine'),
    ('2', 'Referral'),
    ('3', 'Social Media'),
    ('4', 'Other'),
)

class ContactForm(forms.Form):
    contact_name = forms.CharField(label='Name', required=True)
    contact_email = forms.EmailField(label='E-mail', required=True)
    contact_phone = forms.CharField(label='Phone number', required=True, max_length=15)
    move_date = forms.DateField(label='Date Requested', required=False)
    address_from = forms.CharField(label='Address From', required=False)
    address_to = forms.CharField(label='Address To', required = False)
    contact_access = forms.ChoiceField(choices=ACCESS_CHOICES, label='Is there restrictive access to either address that we should be aware of? (stairs, narrow drive, etc.)')
    additional_information = forms.CharField(label='Additional Information', max_length=250, required=False)
    contact_hear = forms.ChoiceField(choices=HEAR_CHOICES, label='How did you hear about us?', required=False)

contact_us.html

{% extends 'app01/base.html' %}
{% block body %}
<form action="" method="post">
{% csrf_token %}
{{ form }}
    <input type="submit" value="submit" />
</form>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

我第一次将Django应用程序创建者作为第一次发送错误的电子邮件。只需添加:

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

到settings.py ....(许多文档只指向实现这些行)

即使您只是按照我的意愿发布到后端,您仍然需要SMTP来发送电子邮件内容。 (在将网站投入生产之前测试成功发送电子邮件)。

SMTP选项:
我看到了一些不同的SMTP选项。谷歌为我的测试工作。我计划在进入生产后切换到Sendgrid。我之前没有使用Sendgrid的原因是因为我还没有注册域名。拥有一个注册域名需要使用sendgrid作为你的SMTP,我还没有我的。

那么我在项目中添加了什么来将Google SMTP电子邮件发送到控制台后端呢? Settings.py

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemailaddress@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

执行此操作后,请登录您的Gmail帐户,转到:我的帐户&gt;设置&gt;登录和安全
转动允许不太安全的应用程序开启。
作为我研究的旁注,我发现可以使用谷歌SMTP进行离线测试,但是在生产中使用它是违法的。将网站投入生产后,必须使用其他SMTP服务,例如SendGrid。此外,您还需要确保删除EMAIL_BACKEND,因为正如jpmc26指出的那样,您不希望将电子邮件数据记录在生产中。