django.core.mail.send_mail Example Code

send_mail is a function in Django that can send an email using the EmailMessage class.

Example 1 from apiserver

apiserver is a RESTful web API server project built with Django for user management tasks such as registration (with email verification), login, logout and password changes.

apiserver/apps/accounts/signals.py

from django.conf import settings
from django.template import Context, loader
from django.core.mail import send_mail,EmailMessage
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.template.loader import get_template
from django.contrib.auth.decorators import login_required
from django.db.models.signals import post_save
from django.dispatch import receiver
from apps.accounts.models import UserProfile, create_api_key


from django.db import models
models.signals.post_save.connect(create_api_key, sender=UserProfile)

@receiver(post_save,sender=UserProfile)                
def send_contact_mail(sender, created, **kwargs):
    obj = kwargs['instance']
    if created:
        subject = "Thank you for registering with us"
        to = [obj.email]
        verification_code = obj.activation_token
        val =  { 
                'site_url': settings.SITE_URL,
                'subject':subject,
                'verification_code':verification_code,
                }

        html_content = render_to_string('mails/registration.html',val)
        text_content = strip_tags(html_content)
        from_email = settings.DEFAULT_FROM_EMAIL
        msg_html = render_to_string('mails/registration.html',val)
        send_mail(subject, None, from_email,to,html_message=msg_html)
1. Introduction 2. Development Environments 3. Data 4. Web Development 5. Deployment 6. DevOps Changelog What Full Stack Means About the Author Future Directions Page Statuses Django ExtensionsDjango Example Codedjango.apps.config AppConfigdjango.conf settingsdjango.conf.urls.urldjango.contrib.admindjango.contrib.admin.filters SimpleListFilterdjango.contrib.admin.sites registerdjango.contrib.auth.decorators login_requireddjango.contrib.auth get_user_modeldjango.contrib.auth.hashers make_passworddjango.core.exceptions ImproperlyConfigureddjango.core.mail.messages EmailMessagedjango.core.mail.send_maildjango.core.management.base BaseCommanddjango.db.models AutoFielddjango.db.models BooleanFielddjango.db.models CharFielddjango.db.models DateFielddjango.db.models DateTimeFielddjango.db.models FileFielddjango.db.models ForeignKeydjango.db.models GenericIPAddressFielddjango.db.models ImageFielddjango.db.models IntegerFielddjango.db.models Modeldjango.db.models PositiveIntegerFielddjango.db.models PositiveSmallIntegerFielddjango.db.models.signaldjango.db.models SlugFielddjango.db.models SmallIntegerFielddjango.db.models TextFielddjango.db OperationalErrordjango.dispatch Signaldjango.formsdjango.forms BooleanFielddjango.forms CharFielddjango.forms ChoiceFielddjango.forms EmailFielddjango.forms IntegerFielddjango.http Http404django.http HttpResponsedjango.http HttpResponseBadRequestdjango.http HttpResponseForbiddendjango.http HttpResponseNotModifieddjango.http HttpResponsePermanentRedirectdjango.http HttpResponseRedirectdjango.template.response SimpleTemplateResponsedjango.template.response TemplateResponsedjango.urls.pathdjango.urls reverse_lazydjango.urls.exceptions NoReverseMatchdjango.urls.exceptions Resolver404django.utils.html format_htmldjango.utils.timezone ...or view the full table of contents.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2020