13.11. Creating a Django Project: Sending Emails

Página 69

13.11. Creating a Django Project: Sending Emails

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 = '[email protected]'
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:

from django.core.mail import send_mail

send_mail(
    'Email Subject',
    'Email body',
    '[email protected]',
    ['[email protected]'],
    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',
    '[email protected]',
    ['[email protected]'],
    ['[email protected]'],
    reply_to=['[email protected]'],
    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', '[email protected]', ['[email protected]'])
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.

Next page of the Free Ebook:

7013.12. Creating a Django Project: Integration with APIs

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text