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.


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)

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

        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)

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-2019