
How to Send Emails in Django with Examples
April 23, 2023
Sending emails in Django is a crucial part of many web applications. In this tutorial, we will learn how to send emails in Django using the built-in EmailMessage class, third-party libraries such as Django's EmailMessage class, and Django's send_mail function. We will cover everything from setting up your email configuration to creating email templates.
Before we begin, make sure you have Django installed in your system. If not, please install it using the following command:
pip install django
Also, make sure that you have a working SMTP server, which is required for sending emails.
Setting up Email Configuration
To send emails in Django, you need to define your email configuration in the settings.py file. Here's an example configuration for Gmail:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your_email@gmail.com'
EMAIL_HOST_PASSWORD = 'your_generate_password'
The EMAIL_BACKEND
setting specifies the backend that will be used for sending emails. The default backend is django.core.mail.backends.smtp.EmailBackend
, which is used in the above configuration.
The EMAIL_HOST
setting specifies the SMTP server that will be used to send emails. In the above example, we're using Gmail's SMTP server.
The EMAIL_PORT
setting specifies the port number for the SMTP server. In the above example, we're using port number 587.
The EMAIL_USE_TLS
setting specifies whether to use TLS (Transport Layer Security) for encrypting the connection. In the above example, we're using TLS.
The EMAIL_HOST_USER
setting specifies the username for the email account that will be used to send emails.
Note: Make sure that the click here to generate your app password. Otherwise, you will face an SMTP error.
The EMAIL_HOST_PASSWORD
setting specifies the password for the email account that will be used to send emails.
Sending Emails using EmailMessage
The EmailMessage
class is a built-in class in Django that can be used to send emails. Here's an example of how to use it:
from django.core.mail import EmailMessage
email = EmailMessage(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
['bcc@example.com'],
reply_to=['another@example.com'],
headers={'Message-ID': 'foo'},
)
email.send()
In the above example, we're creating an EmailMessage
object with the following parameters:
subject
: The subject of the email.body
: The body of the email.from_email
: The email address from which the email is sent.to
: A list of email addresses to which the email is sent.bcc
: A list of email addresses to which the email is sent as a blind carbon copy (BCC).reply_to
: A list of email addresses to which replies should be sent.headers
: Additional headers to include in the email.
Finally, we're calling the send()
method on the email
object to send the email.
Sending Emails using send_mail
Django also provides a send_mail
function, which is a simpler way to send emails. Here's an example of how to use it:
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
In the above example, we're calling the send_mail
function with the following parameters:
subject
: The subject of the email.message
: The message of the email.from_email
: The email address from which the email is sent.recipient_list
: A list of email addresses to which the email is sent.fail_silently
: A Boolean value that specifies whether errors should be raised or not.
Sending Emails with Attachments
You can also send emails with attachments using the EmailMessage
class. Here's an example:
from django.core.mail import EmailMessage
from django.core.files.base import ContentFile
file_content = ContentFile('file content goes here')
email = EmailMessage(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
)
email.attach('file.txt', file_content.read(), 'text/plain')
email.send()
In the above example, we're creating an EmailMessage
object, and then attaching a file to it using the attach()
method. The attach()
method takes the following parameters:
filename
: The name of the file.content
: The content of the file.mimetype
: The MIME type of the file.
Sending Emails with HTML Content
You can also send emails with HTML content using the EmailMessage
class. Here's an example:
from django.core.mail import EmailMessage
email = EmailMessage(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
)
email.content_subtype = 'html'
email.body = '<html><body><h1>Hello, World!</h1></body></html>'
email.send()
In the above example, we're setting the content_subtype
attribute of the EmailMessage
object to 'html'
, and then setting the body
attribute to the HTML content.
Sending Emails with Templates
You can also send emails using templates in Django. Here's an example:
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
html_content = render_to_string('email_template.html', {'name': 'John'})
text_content = strip_tags(html_content)
email = EmailMultiAlternatives(
'Subject here',
text_content,
'from@example.com',
['to@example.com'],
)
email.attach_alternative(html_content, 'text/html')
email.send()
In the above example, we're using the EmailMultiAlternatives
class, which allows us to send both HTML and plain text versions of the email. We're also using the render_to_string
function to render the email template, and the strip_tags
function to extract the plain text version of the email.
Conclusion
Sending emails is an important part of many web applications. In this tutorial, we've learned how to send emails in Django using the built-in EmailMessage
class, as well as third-party libraries such as send_mail
and EmailMultiAlternatives
. We've also learned how to send emails with attachments, HTML content, and templates. With these tools, you can easily implement email functionality in your Django web application.