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:
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.