簡體   English   中英

Django 發送帶有圖像的 HTML 電子郵件

[英]Django Sending HTML Email with Images

我正在嘗試使用 django 1.3 中的圖像發送 HTML 電子郵件,但我的圖像未加載。 以下是我的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, '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>

該圖像位於我的應用程序文件夾的 static/image 文件夾中。 但是當收到電子郵件時,它沒有圖像。

在我的 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/'

請回答我缺少什么?

謝謝,

正如漫游所提到的,您缺少主機名+端口。

對於那些使用 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="" />

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM