How to create email templates in django
Tags: webdev, tutorial, python, django
Published: 2023-12-27 23:16:18

**Introduction**
Django is a popular and capable Python web development framework that simplifies building web apps. One of Django's many useful capabilities is its strong tools for sending emails.

With Django, you can set up email backends and make reusable HTML email templates for your app. In this blog article, we'll go through how to make HTML email templates in Django and send emails using those templates.

Emailing users is a frequent activity for many web applications. You may need to send emails for account verification, password resets, invoices, and more. 

Creating unique HTML emails from scratch for every situation can be tiresome and time-intensive. This is where templates are useful! With templates, you can make your base email HTML once and reuse it for all emails.

**Prerequisites**
Terminal/ Command line
Python
Intermediate Django
HTML

**Configuring email settings**
To begin making templates, we first need to set up Django's email backend configurations. These settings tell Django how to send emails. The most popular method is to use SMTP and connect Django to an external email provider such as Mailgun, SendGrid, Amazon SES, or your own SMTP server.

The email backend settings can be specified in Django's settings.py file. For example, here is how to configure SMTP with Mailgun:

```
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.mailgun.org'  
EMAIL_HOST_USER = 'your-mailgun-username'
EMAIL_HOST_PASSWORD = 'your-mailgun-password'
EMAIL_PORT = 587 
EMAIL_USE_TLS = True
```

This connects Django to Mailgun for sending emails. Make sure to use your own Mailgun credentials. You can use a similar approach for other SMTP providers.

**Creating templates**

After configuring your email backend, you can start creating templates. Email templates in Django are just normal Django templates that live under a templates/registration/ directory in your app.

an example email_template.html under templates/registration/:

```
<!DOCTYPE html>
<html>
<head>
<title>My Email Template</title>
</head>
<body>

<p>Hi {{ name }},</p>

<p>This is a test email template from my Django app. Welcome!</p>

<p>Thanks,<br>  
The App Team
</p>

</body>
</html>
```
This is a simple HTML template that we can reuse for all emails.  We can insert variables like `{{ name }}` that get replaced dynamically when sending emails.

**Sending templated emails in django views**

Once the template is set up, utilizing it in the views is easy. First, bring in render_to_string to render the template as a string:

```
from django.core.mail import EmailMultiAlternatives 
from django.template.loader import render_to_string

# Render template as a string
html_content = render_to_string('registration/email_template.html', {'name': 'John'})
```

Afterward, make an EmailMultiAlternatives instance, add the rendered template to it, and invoke the .send() method:

```
msg = EmailMultiAlternatives(
subject='Test Email',
body='This is a test',
from_email='from@example.com',
to=['to@example.com']
)
msg.attach_alternative(html_content, 'text/html')

msg.send()


```
All done!

**Conclusion**

The ability to rapidly generate and send email templates is essential for most web applications built with Django. Thanks to Django's robust email backends and template engine, sending customizable HTML emails is easy. To start, you need to set up your email settings. Next, build reusable templates. 

Then, leverage Django's EmailMultiAlternatives class in your views to send template emails. On top of that, adhere to best practices such as using table layouts and testing across email clients for the most robust emails.

In summary, with some configuration and planning, sending great emails with Django is straightforward.