Reputation: 459
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.
Here's my console output:
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
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.')
This is 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'),
]
this is forms.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
)
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 %}
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?
Thanks
Upvotes: 0
Views: 5571
Reputation: 308849
The console backend, as the name suggests, prints out the email to the console and doesn't do anything else.
You need to use a different email backend, for example the smtp backend is the default:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
You'll then have to configure your settings like EMAIL_HOST
with your email provider's settings. See the docs for more info.
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.
Upvotes: 3