Email notifications are a crucial part of many web applications. They allow you to keep users informed about important events, like account changes, password resets, or order confirmations. Django provides a powerful and flexible framework for sending emails, making it easy to integrate email functionality into your projects.
Before we dive into the code, make sure you have the following:
Django uses email backends to send emails. The default backend is the `django.core.mail.backends.smtp.EmailBackend`, which allows you to send emails through a SMTP server.
To configure the email backend in your Django project, open the `settings.py` file and modify the following:
```python EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'your_password' EMAIL_USE_TLS = True ```Replace the placeholders with your actual email credentials. For Gmail, you'll need to enable "Less secure app access" in your Google Account settings.
Now that your email backend is configured, let's send a simple email:
```python from django.core.mail import send_mail def send_welcome_email(email, name): subject = 'Welcome to Our Website!' message = f'Hi {name}, \n\nWelcome to our website! We are excited to have you join us.' from_email = '[email protected]' recipient_list = [email] send_mail(subject, message, from_email, recipient_list) ```This code sends a welcome email to the specified recipient, using the `send_mail()` function. The `subject`, `message`, `from_email`, and `recipient_list` parameters are self-explanatory.
You can call this function in your views or anywhere else in your code to send an email.
For more complex email notifications, you can use email templates. Django provides a powerful templating engine that allows you to create reusable templates for different email types.
Create a new directory called `templates/email` in your project directory. Then, create a template file called `welcome_email.html` inside this directory.
```htmlHi {{ name }},
Welcome to our website! We are excited to have you join us.
Sincerely,
The Team
```Now, modify your `send_welcome_email` function to use the template:
```python from django.core.mail import EmailMessage def send_welcome_email(email, name): subject = 'Welcome to Our Website!' message = render_to_string('email/welcome_email.html', {'name': name}) from_email = '[email protected]' recipient_list = [email] email = EmailMessage(subject, message, from_email, recipient_list) email.content_subtype = "html" email.send() ```We use the `render_to_string()` function to render the template with the `name` variable. We then create an `EmailMessage` object and set the `content_subtype` to `html` to indicate that the message is in HTML format.
This approach allows you to easily create and manage your email templates, making your code cleaner and more maintainable.
In this part, we've learned the fundamentals of setting up email notifications in Django. We covered configuring the email backend, sending simple emails, and using email templates for more customization. In the next part, we'll explore more advanced features like attachments and custom email context.