【问题标题】:Django Sending HTML Email with ImagesDjango 发送带有图像的 HTML 电子邮件
【发布时间】:2013-11-11 17:48:39
【问题描述】:

我正在尝试发送包含来自 django 1.3 的图像的 HTML 电子邮件,但我的图像没有加载。以下是我的观点.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, 'admin@company.com', ['user@gmail.com'])
msg.content_subtype = "html"
msg.send()

我的模板('user_email.html')如下所示

<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>

图像位于我的应用程序文件夹的静态/图像文件夹中。但是当收到电子邮件时,它没有图像。

在我的 settings.py 中,我有以下内容

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/'

请回复我缺少的内容?

谢谢,

【问题讨论】:

标签: html django email templates static


【解决方案1】:

正如 roam 所说,您缺少主机名+端口。

对于那些使用 Django 的 allauth 包进行身份验证的人(https://www.djangopackages.com/packages/p/django-allauth/),您可以简单地使用 {{site}},例如:

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

或者更好的是,使用 %static 注释:

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

【讨论】:

    【解决方案2】:

    您需要指定完整的STATIC_URL,例如http://localhost:8000/static/。现在消息指向/static/images/footer.jpg,这不是一个有效的 URL。

    【讨论】:

      猜你喜欢
      • 2014-12-24
      • 2011-04-16
      • 2013-06-01
      • 2012-04-07
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多