django.core.mail.messages EmailMessage Example Code

The EmailMessage class is contained with the django.core.mail module within the Django project code base.

Example 1 from django-emailmessagetemplate

django-emailmessagetemplates is a code library that makes it easier to add functionality for end users to customize email templates in a Django application. The code is available under the BSD 3-Clause "New" or "Revised" License.

django-emailmessagetemplates/emailmessagetemplates/utils.py

import copy

from django.core.mail import get_connection
from django.conf import settings

from models import EmailMessageTemplate


def send_mail(name, related_object=None, context={}, from_email=None,
              recipient_list=[], fail_silently=False, auth_user=None,
               auth_password=None, connection=None):
    """
    Easy wrapper for sending a single templated message to a recipient list.  
    The template to use is retrieved from the database based on the name and 
    related_object (optional) fields.
    All members of the recipient list will see the other recipients in the 'To' 
    field.
    If auth_user is None, the EMAIL_HOST_USER setting is used.
    If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
    """

    template = EmailMessageTemplate.objects.get_template(name, related_object)

    connection = connection or get_connection(username=auth_user,
                                              password=auth_password,
                                              fail_silently=fail_silently)

    template.context=context
    template.from_email=from_email
    template.to=recipient_list
    template.connection=connection

    return template.send()

Sign up for a monthly email with Full Stack Python tutorials. No spam ever.


Matt Makai 2012-2019