显示图像以在django中发送电子邮件

时间:2016-05-24 11:11:06

标签: html django email

我想发送一封电子邮件,其中包含发送的HTML电子邮件,如果我的HTML内容包含图片且未显示,则可以使用文本。 此外,如果我尝试查看本地图片中的HTML模板是否正确显示。 如何在HTML内容中显示图像?

HTML内容

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
    <style>
        body{
            background-color: gray;
            color: red;
        }
        div, #globale{
            background-color:#eee;
        }
    </style>
</head>
<body>
        <!--<img src=" {% static "Kamal.png" %}" alt="image"/>-->
        <!-<img src="{% static "Kamal.png" %}" alt="image"/>-->
        <div id="globale">
            <p>
                Convert the filename, content, mimetype triple into a MIME attachment
                object. Adjust headers to use Content-ID where applicable.
                Taken from <img src="Kamal.png" alt=" Mon Image "/>
                HTML is the method of choice for those wishing to send emails with rich
                text, layout and graphics. Often it is desirable to embed the graphics
                within the message so recipients can display the message directly,
                without further downloads.
                <img src="{% static "Kamal.png" %}" alt=" Mon Image "/>
                Some mail agents don't support HTML or their users prefer to receive
                plain text messages. Senders of HTML messages should include a plain
                text message as an alternate for these users.
                <img src="Kamal.png" />
                This recipe sends a short HTML message with a single embedded image
                and an alternate plain text message.
            </p>
            <h3>Message Reussi avec Image</h3>
        </div>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

首先,它不能在localhost中工作......图像不会附加到电子邮件中 ​​- 实际上会发生什么是电子邮件中的html引用你的图像服务器,例如(http://example.com/static/Kamal.png

话虽如此,您可以设置完整路径STATIC_URL,如:

STATIC_URL = 'http://example.com/static/'

而不仅仅是:

STATIC_URL = '/static/'

另一个选项是使用get_host()方法构建您的网址,以防您有STATIC_URL = '/static/'

<img src="{{ request.get_host }}{% static 'Kamal.png' %}" alt=" Mon Image "/>

https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.HttpRequest.get_host

答案 1 :(得分:0)

发送邮件之前发送域名:

site=Site.objects.filter(id=1)
domain = site[0].domain //You will get your domain name 'xyz.com'
subject ="Conform your email address "
message = render_to_string('EmailTemplates/Email_verification.html', {
                    'email':user.email,
                    'user_display':user.first_name,
                    'domain':"{0}".format(domain),//It will return
                                    your doman 'http://127.0.0.1:8000 or etc'
                     })
                send_email(subject, message, user
)

您的模板在静态网址之前使用域名:

<img src="{{domain}}/static/DataSearch/images/slider-image.png" alt="Party Wumpus" title="None" width="500" style="height: auto;">