Django联系表格发送电子邮件

Django联系表格发送电子邮件

本文介绍了Django联系表格发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个联系表格,以便在用户填写电子邮件时向我发送电子邮件。一切似乎都正常,但我没有收到电子邮件。

I created a contact form to send me an email when the user fills it out. Everything appears to be working, but I'm not getting an email.

这是我的控制台输出:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Michael Plemmons
From: [email protected]
To: [email protected]
Date: Thu, 02 Nov 2017 22:40:50 -0000
Message-ID: <20171102224050.12741.24539@ubuntu>

hello this is a test
-------------------------------------------------------------------------------
[02/Nov/2017 22:40:50] "POST /contact/ HTTP/1.1" 302 0
[02/Nov/2017 22:40:50] "GET /success/ HTTP/1.1" 200 36

这是views.py

This is views.py

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from .models import *
from .forms import contact_form
from django.core.mail import send_mail, BadHeaderError
from django.shortcuts import redirect

def contact(request):
    if request.method == 'GET':
        form = contact_form()
    else:
        form = contact_form(request.POST)
        if form.is_valid():
            contact_name = form.cleaned_data['contact_name']
            contact_email = form.cleaned_data['contact_email']
            contact_message = form.cleaned_data['contact_message']
            try:
                send_mail(contact_name, contact_message, contact_email, ['[email protected]'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('success')
    return render(request, "contact.html", {'form': form})


def success(request):
    return HttpResponse('Success! Thank you for your message.')

这是urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'contact/$',views.contact, name='contact'),
    url(r'^success/$', views.success, name='success'),
]

这是form.py

from django import forms

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User

class contact_form(forms.Form):
    contact_name = forms.CharField(label='Contact Name', max_length=255)
    contact_email = forms.CharField(label='Contact Email',max_length=255)
    contact_message = forms.CharField(
        required=True,
        widget=forms.Textarea
    )

和contact.html

and contact.html

{% block title %}Contact - {{ block.super }}{% endblock %}

{% block content %}
<h1>Contact</h1>
<form role="form" action="" method="post">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Submit</button>
</form>
{% endblock %}

这包括在settings.py

And this included in settings.py

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

有什么我想让它发送到我的电子邮件吗?

Is there anything I am missing to get this to actually go to my email?

谢谢

推荐答案

,将电子邮件打印到控制台,不执行其他任何操作。

The console backend, as the name suggests, prints out the email to the console and doesn't do anything else.

您需要以使用其他电子邮件后端,例如。

You'll then have to configure your settings like EMAIL_HOST with your email provider's settings. See the docs for more info.

如果您不想使用SMTP后端,另一种常见选择是使用诸如Mailgun或SendGrid之类的事务性邮件提供程序。其中一些服务具有免费使用级别,对于低数量的联系表,这应该足够了。 应用程序支持多个事务邮件提供商。

If you don't want to use the SMTP backend, another common choice is to use a transactional mail provider like Mailgun or SendGrid. Some of these services have free usage tiers which should be sufficient for a low-volume contact form. The django-anymail app supports several transactional mail providers.

这篇关于Django联系表格发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:51