liveBook · Manning

Send_mail()

As explained in Django in Action

send_mail() is a Django function that sends an email through the configured email backend.

send_mail() is Django’s quick path for sending an email through the currently configured email backend. A validated form supplies the message content, while the function receives a subject, message, sender, and recipient list. In development, the backend can print the email to the console instead of delivering it. section 7.1.1 section 7.6

Email a submitted comment

from django.core.mail import send_mail if form.is_valid(): name = form.cleaned_data["name"] comment = form.cleaned_data["comment"] message = f"Received comment from {name}\n\n{comment}" send_mail( "Received comment", message, "admin@example.com", ["admin@example.com"], fail_silently=False, ) After is_valid() creates cleaned_data, the view builds the message and calls send_mail(). Setting fail_silently=False makes delivery problems raise an exception rather than disappearing quietly; the development server reports the problem as a 500 error, while production logs it. section 7.1.1

Listing 7.2 A view accepting both HTTP GET and POST to handle comment form data
# RiffMates/content/views.py
from django.core.mail import send_mail
from django.shortcuts import render, redirect

from content.forms import CommentForm  #1

def comment(request):
    if request.method == 'GET':
        form = CommentForm() #2

    else: # POST  #3
        form = CommentForm(request.POST)  #4

        if form.is_valid():  #5
            name = form.cleaned_data["name"]  #6
            comment = form.cleaned_data["comment"]

            message = f"""\
                Received comment from {name}\n\\n
                {comment}
            """

            send_mail("Received comment", message, #7
                "admin@example.com", ["admin@example.com"],
                fail_silently=False) #8
            return redirect("comment_accepted")  #9


    # Was a GET, or Form was not valid #10
    data = {
        "form": form,
    }

    return render(request, "comment.html", data)

Use the complete view listing to follow the GET-and-POST request flow: display the form, validate submitted data, send the email, and redirect after success. section 7.1.1

How Django in Action covers it

How deep it goes
Explains the call arguments, cleaned_data source, configured backend, and fail-silently behavior, but not backend delivery internals.
Examples
Uses a RiffMates comment form to email the administrator the submitter’s name and comment.
What it assumes
Assumes basic Django views and forms while establishing validation, cleaned_data, and email-backend behavior in the workflow.
What you can do after
Apply it via Django to send validated form content and expose delivery failures during development or production logging.
How it connects
Connects email sending with Django forms, cleaned_data, HTTP POST handling, validation, and configured email backends.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.