Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 865
Muhammad Abbas Avatar asked Oct 31 '13 15:10

Muhammad Abbas


2 Answers

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="" />
like image 120
o_c Avatar answered Oct 24 '22 07:10

o_c


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.

like image 3
roam Avatar answered Oct 24 '22 07:10

roam