Creating a Django Project: Sending Emails

Capítulo 69

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

When building a system with Python and Django, one of the most useful features you can implement is the ability to send emails. Whether it's notifying users of updates, confirming subscriptions, or resetting passwords, sending emails is an essential tool for any web application. In this chapter, we'll explore how to configure Django to send emails.

Email Server Configuration

To send email with Django, you need an SMTP mail server. Django supports many mail servers such as Gmail, SendGrid, Amazon SES, and others. For the purpose of this tutorial, we're going to use Gmail.

First, you need to configure email settings in your settings.py file. Here is an example of how to configure Gmail:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'yourmail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'

The above settings are self-explanatory. EMAIL_HOST and EMAIL_PORT are the Gmail SMTP server settings. EMAIL_USE_TLS is set to True because Gmail uses TLS. EMAIL_HOST_USER and EMAIL_HOST_PASSWORD are your Gmail email and password respectively.

Sending Emails

With your email server settings configured, you can now send emails. Django provides a function called send_mail for sending emails. Here is an example of how to use the send_mail function:

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

from django.core.mail import send_mail

send_mail(
    'Email Subject',
    'Email body',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)

The send_mail function accepts five parameters. The first is the email subject. The second is the body of the email. The third is the sender's email address. The fourth is a list of recipients' email addresses. The last parameter, fail_silently, is a boolean. If set to True, the function will not throw exceptions if the email cannot be sent. If False, the function will throw exceptions if the email cannot be sent.

Send Emails with HTML

Django also supports sending HTML emails. To send an email with HTML, you can use the EmailMessage function. Here is an example of how to use the EmailMessage function to send an email with HTML:

from django.core.mail import EmailMessage

email = EmailMessage(
    'Hello',
    'Body goes here',
    'from@example.com',
    ['to@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},
)

email.send()

The EmailMessage function accepts several parameters. The first four are the same as the send_mail function. The fifth parameter is a list of email addresses to BCC. The sixth parameter is a list of email addresses for the reply. The last parameter is a dictionary of extra headers for the email.

To add HTML to email, you can use the content_subtype method of the EmailMessage object. Here is an example:

email = EmailMessage('Hello', 'Body goes here', 'from@example.com', ['to@example.com'])
email.content_subtype="html"
email.send()

I hope this chapter has given you a good understanding of how to send email with Django. In the next chapter, we'll explore how to integrate Django with databases.

Now answer the exercise about the content:

Which Django function is used to send emails and what are its parameters?

You are right! Congratulations, now go to the next page

You missed! Try again.

The correct function for sending emails in Django is the send_mail function. It uses the following parameters: email subject, email body, sender email, list of recipient emails, and a boolean fail_silently parameter.

Next chapter

Creating a Django Project: Integration with APIs

Arrow Right Icon
Free Ebook cover System creation course with Python and Django complete
39%

System creation course with Python and Django complete

New course

176 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.