[PYTHON] Django Contact Form 2

Premise

Continuing from the previous session.

https://qiita.com/yusuke_mrmt/items/ebefdb6e8704913f6a4c

Main subject

Set the app password in Gmail.

Set up two-step verification.

First, open the Gmail app and tap the menu bar in the upper left. There is ** Settings ** at the bottom, so tap it.

Tap ** Manage Google Accounts **.

Select the ** Security ** item and select ** Account protection ** and proper settings Let's make settings such as two-step verification.

The obtained password is used only for the application, so as Google says I think you don't have to take any measures such as keeping it somewhere. However, since it is described in the Django configuration file ** settings.py </ code> ** Copy it to a text editor.

Add mail sending function

We'll be adding Django's email sending capabilities.

First, in the configuration file ** settings.py </ code> ** I will describe how to use the mail server.

When posting code on GitHub, use a .gitignore </ code> file, etc. Make sure to give the exclusion completely.

project/project/settings.py


STATIC_URL = '/static/'

#For mail server
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'Obtained app password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Then in views.py </ code> Process the message written in the inquiry form I will add it so that I can respond appropriately.

project/contact/views.py



from django.shortcuts import render, redirect
from .forms import ContactForm
from django.http import HttpResponse #Postscript
from django.conf import settings #Postscript
from django.core.mail import BadHeaderError, send_mail #Postscript

.........

def contact_form(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            #Postscript
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            sender = form.cleaned_data['sender']
            myself = form.cleaned_data['myself']
            recipients = [settings.EMAIL_HOST_USER]
            if myself:
                recipients.append(sender)
            try:
                send_mail(subject, message, sender, recipients)
            except BadHeaderError:
                return HttpResponse('An invalid header was found.')
            return redirect('contact:complete')
    else:
        form = ContactForm()
    return render(request, 'contact/contact_form.html', {'form': form})

.........

To format and return the data validated with form.is_valid () </ code> Store it in a variable using cleaned_data [] </ code>.

recipients </ code> is the configuration file I am passing my address defined in settings.py.

send_mail </ code> requires 4 arguments. Send subject / message / sender to your own address.

  • The sender (sender's address) will not be displayed in your email inbox, so change it later.

BadHeaderError </ code> is a description to prevent header injection.

if_myself </ code> is when the sender checks the checkbox The sender's address (sender) is added to recipients and an email is sent to both parties.

The sender of the mail sent to both sides will be from the set own address.

Let's send it once. If you are redirected to complete transmission, you are successful.

Recommended Posts