Html email templates with django

How can I send HTML emails with embedded images in django application? How can I embed images like logos which will be displayed in the emails sent?

I tried using {% load static %} in my html email template, but still no luck

I think you should use the "WeasyPrint" library. It works by creating an HTML file where you can pass in variables (like images from statics, text, or anything else), and finally, you can send it to people via email.


pip install weasyprint

from weasyprint import HTML

html_string = render_to_string('invoice.html', order_data)

html = HTML(string=html_string)
pdf_file = BytesIO()
html.write_pdf(pdf_file)

pdf_content = pdf_file.getvalue()

send_email(
    'sender',
    'subject',
    'body',
    pdf_content
)

I hope I was able to help you.

ask gpt for more questions..

Since the email will render on other servers (like Gmail), you shouldn't use a relative address to load an image in the email template and should use the full address with hostname to load the image in the email.

We use {% load_static %} in regular templates to address and load images. e.g. :

<img src={% static "images/logo.png" %}>

But in the email templates, we should add hostname instead of using the tag static. e.g. :

<img src="{{YOUR_HOST_NAME}}/static/images/logo.png">

I managed to get an easy way to solve the problem, which is to pass the images as variables to to your email template. Quick brief here I was sending mulitple emails to users, the email has files[imageA.jpg, imageB.jpg] attached to it as well as some static images in it [img1.jpg, img2.jpg]

def send_notice_email(sender, instance, created, **kwargs):
    
 if created:
    all_users = CustomUser.objects.all()

    subject = **********
    context = {
      'title': ********,
      'content': ********,
            }
    
    # List of image paths to attach
    image_paths = [
      'static/images/imageA.jpg',
      'static/images/imageB.jpg',
       ]
            
     for user in all_users:
    
        context['user'] = user 
                
        html_content=render_to_string('yourappname/email_template.html', 
        context)
        text_content = strip_tags(html_content)
    
        email_message = EmailMultiAlternatives(
            subject=subject,
            body=text_content,
            from_email='youremail@domain',
            to=[user.email],
)
    
# Attach images
for image_path in image_paths:
    with open(image_path, 'rb') as img:
       image_name = image_path.split('/')[-1]
       email_message.attach(image_name, img.read(), 'image/jpeg')  
    
html_content = html_content.replace('src="img1.jpg"', 'src="cid:img1.jpg"')
html_content = html_content.replace('src="img2.jpg"', 'src="cid:img2.jpg"')
    
email_message.attach_alternative(html_content, 'text/html')

email_message.send()

In your html email template the pictures you will include in the following manner:

<img src="{cid:img1.jpg}" alt="img1">
<img src="{cid:img2.jpg}" alt="img2">

Because {% load static %} will not work for email template, Use the below format

https://www.your-domain.com/static/path_to_image/image_file_name.extension

Back to Top