[PYTHON] Send email with Django

Introduction

Here, we will explain how to send an email with Django. It can be used to create an inquiry form.

Default settings

First, write the mail-related settings in settings.py. The following is an example when using Gmail.

settings.py


EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD =Password for the app
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Email subject and recipient settings

Here, in the inquiry form Define a function for sending mail in forms.py.

forms.py


from django import forms
from django.conf import settings
from django.core.mail import BadHeaderError, EmailMessage
from django.http import HttpResponse


def send_email(self):
        subject = 'Contact Us:' + self.cleaned_data['title']
        name = self.cleaned_data['name']
        email = self.cleaned_data['email']
        from_email = '{name} <{email}>'.format(name=name, email=email)
        message = self.cleaned_data['body']
        recipient_list = [settings.EMAIL_HOST_USER]
        email_message = EmailMessage(subject, message, from_email, recipient_list, reply_to=[email])
        try:
            email_message.send()
        except BadHeaderError:
            return HttpResponse('An invalid header has been detected.')

ʻIf you do not set reply_to when creating an EmailMessage` instance, you will not be able to reply by email because you do not know the email address of the person who made the inquiry, so be sure to enter it.

Summary

Here, I explained the settings required when sending an email with Django. It can be applied to other than the inquiry form.

Recommended Posts

Send email with Django
Note: Send an email with Django
Send email with Python
Send Japanese email with Python3
[Automation] Send Outlook email with Python
[Python] Send an email with outlook
Easily send emails with Gmail with Django
Send email via gmail with Python 3.4.3.
Internationalization with django
[Python] Send email
[Python] Send email
CRUD with Django
HTML email with image to send with python
Send an email with Amazon SES + Python
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Send an email to Spushi's address with python
Twitter OAuth with Django
Getting Started with Django 1
File upload with django
Use LESS with Django
I tried to send a registration completion email from Gmail with django.
Validate E-Mail with Python
Pooling mechanize with Django
Use MySQL with Django
Send an email with Excel attached in Python
Start today with Django
Getting Started with Django 2
Send an email with a user other than EMAIL_HOST_USER written in settings in django
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Get started with Django! ~ Tutorial ⑤ ~
Minimal website environment with django
Create an API with Django
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Create a homepage with django
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Send email in Python (Outlook)
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
Do AES encryption with DJango
Getting Started with Python Django (6)
Send email using Python's smtplib
Combine two images with Django
Getting Started with Django with PyCharm
Real-time web with Django Channels
Double submit suppression with Django
Django REST framework with Vue.js
Use prefetch_related conveniently with Django
Getting Started with Python Django (5)