Simple Steps to Send Emails with Static Images in Django
Tags: django, email, staticfiles, templates
Published: 2024-01-17 17:24:01

---
title: Simple Steps to Send Emails with Static Images in Django
published: true
date: 2024-01-09 08:00:00 UTC
tags: django,email,staticfiles,templates
canonical_url: https://diegocarrasco.com/sending-emails-static-images-django/
---

![](https://diegocarrasco.com/images/social-images/sending-emails-static-images-django.jpg)

 

- [Context](https://diegocarrasco.com/sending-emails-static-images-django/#context)
- [Requirements](https://diegocarrasco.com/sending-emails-static-images-django/#requirements)
- [Issue with {% static %} in Emails](https://diegocarrasco.com/sending-emails-static-images-django/#issue-with-static-in-emails)
- [Solution - Generating Full URLs in the View (with code snippets)](https://diegocarrasco.com/sending-emails-static-images-django/#solution-generating-full-urls-in-the-view-with-code-snippets)
  - [In the view](https://diegocarrasco.com/sending-emails-static-images-django/#in-the-view)
  - [In the templates](https://diegocarrasco.com/sending-emails-static-images-django/#in-the-templates)
    - [HTML template](https://diegocarrasco.com/sending-emails-static-images-django/#html-template)
    - [Plain text template](https://diegocarrasco.com/sending-emails-static-images-django/#plain-text-template)
- [Keep in mind](https://diegocarrasco.com/sending-emails-static-images-django/#keep-in-mind)
- [Key Takeaways](https://diegocarrasco.com/sending-emails-static-images-django/#key-takeaways)
- [References](https://diegocarrasco.com/sending-emails-static-images-django/#references)

## Context

I wanted to send an email with a template that included an image using `send_mail` from Django. The image was stored in the `static` folder of the Django project. I wanted to use the Django template system to generate the email content, but I was not sure how to reference the image in the template.

## Requirements

I asume you have a Django project with a static folder and a template that you want to use to generate an email.

## Issue with {% static %} in Emails

The first thing I tried was to use the `{% static %}` template tag to generate the URL for the image. However, this tag does not generate a full URL, but a relative URL. This is not a problem when the template is rendered in the browser, but emails need a full URL to access the image.

This means that `{% static %}` only appends the relative path to the `STATIC_URL`, which is not sufficient for email clients to locate the image.

## Solution - Generating Full URLs in the View (with code snippets)

The solution I found was to generate the full URL in the view and pass it as a context variable to the template.

This involves combining the site's domain with the static file path. This approach uses `get_current_site` to fetch the current site's domain and `static` to get the static file path, concatenating these to form a complete URL.

### In the view

This is an example code to demonstrate how to create the full URL in a Django view:

This URL is then passed to the email template context and used in the src attribute of the img tag.

**Please note that you need to adapt the paths to your project.**

```
# in views.py

# imports we need to create the full URL
from django.contrib.sites.shortcuts import get\_current\_site
from django.templatetags.static import static

# imports we need to render the template to use in the email
from django.template.loader import render\_to\_string

# imports we need to send the email
from django.core.mail import send\_mail

def my\_view(request): # or any other view
    # we get the current site to get the domain
    current\_site = get\_current\_site(request)

    # we get the static file path and concatenate it with the domain
    logo\_url = f'https://{current\_site.domain}{static("images/your-logo.png")}'

    # we generate a context with the variables we want to use in the template
    context = {
                'logo\_url': logo\_url,
                # ... other context variables ...
                }

    subject = 'Subject of your email'

    # we render the templates to use in the email
    message\_plain = render\_to\_string('my\_template.txt', context)
    message\_html = render\_to\_string('my\_template.html', context)

    email = 'test@example.com'

    # we send the email
    send\_mail(subject, message\_plain, None, [email], html\_message=message\_html)

```

### In the templates

When you send emails, it is recommended to provide both a plain text and an HTML version of the email. This is why we have two templates in the view.

#### HTML template

```
<!-- in my\_template.html -->
<img src="{{ logo\_url }}" alt="Your Logo">

```

#### Plain text template

In my case, I did not need to include the image in the plain text version of the email, but if you need to do so, you can use the same approach as in the HTML template.

```
<!-- in my\_template.txt -->
Your Logo: {{ logo\_url }}

```

## Keep in mind

- This approach is not limited to images. You can use it to generate full URLs for any static file.
- This approach is not limited to emails. You can use it to generate full URLs for any static file in any context and render the templates.

## Key Takeaways

- Django's `{% static %}` template tag only generates a relative URL, which is not sufficient for email clients to locate the image.
- To generate a full URL for a static file, you need to combine the site's domain with the static file path.
- You can use `get_current_site` to fetch the current site's domain and `static` to get the static file path, concatenating these to form a complete URL.
- You can use this approach to generate full URLs for any static file in any context and render the templates.
- You construct the full URL in the view and pass it as a context variable to the template.

Happy coding!

## References

- [Django Documentation - Managing static files](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/)
- [Django Documentation - get\_current\_site](https://docs.djangoproject.com/en/4.2/ref/contrib/sites/#get-current-site-shortcut)
- [Django Documentation - static](https://docs.djangoproject.com/en/4.2/ref/templates/builtins/#static)
- [Django Documentation - send\_mail](https://docs.djangoproject.com/en/4.2/topics/email/#send-mail)
- [Django Documentation - render\_to\_string](https://docs.djangoproject.com/en/4.2/topics/templates/#django.template.loader.render_to_string)
- [Django Documentation - EmailMessage](https://docs.djangoproject.com/en/4.2/topics/email/#django.core.mail.EmailMessage)