Muhammad Abbas
Muhammad Abbas

Reputation: 91

Django Sending HTML Email with Images

I am trying to send an HTML email with images from django 1.3 but my Images are not loading. Following is my view.py

email_data = {
# Summary data
'user_name':user_name,
'points':user.numOfPoints,
}
email_data = RequestContext(request, email_data)
t = get_template('user_email.html')
content = t.render(email_data)
msg = EmailMessage(subject='User Email', content, '[email protected]', ['[email protected]'])
msg.content_subtype = "html"
msg.send()

My template('user_email.html') looks like following

<html>
<body>
Hello {{user_name}},
Your point score is: {{points}}
Thank you for using our app.
<img src="{{ STATIC_URL }}images/footer.jpg" alt="" />
<body>
</html>

The image is place inside the static/image folder of my app folder. But when email is received it is without the image.

In my settings.py, I have following

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.messages.context_processors.messages',
)
STATIC_URL = '/static/'

Kindly respond what I am missing?

Thanks,

Upvotes: 9

Views: 12018

Answers (2)

o_c
o_c

Reputation: 4203

As mentioned by roam you are missing the hostname+port.

For those who are using Django's allauth package for authentication (https://www.djangopackages.com/packages/p/django-allauth/), you can simply use {{site}}, e.g.:

<img src="{{site}}{{ STATIC_URL }}images/footer.jpg" alt="" />

Or better yet, using the %static annotation:

{% load static %}
...
<img src="{{site}}{% static 'images/footer.jpg' %}" alt="" />

Upvotes: 8

roam
roam

Reputation: 1345

You'll need to specify the full STATIC_URL, e.g http://localhost:8000/static/. Right now the message points to /static/images/footer.jpg which is not a valid URL.

Upvotes: 4

Related Questions