【问题标题】:Displaying an image to sending email in django显示图像以在 django 中发送电子邮件
【发布时间】:2016-05-24 11:11:06
【问题描述】:

如果我的 HTML 内容包含图像且未显示,我想发送一封包含发送的 HTML 电子邮件的电子邮件,该电子邮件与 cons 的文本相得益彰。 另外如果我尝试查看本地图片中的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>

【问题讨论】:

  • 你的 STATIC_URL 变量中有什么?
  • 在我的 STATIC_URL 变量中我有这个 STATIC_URL = '/static/'

标签: html django email


【解决方案1】:

首先,它在 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

【讨论】:

【解决方案2】:

在发送邮件之前发送域名:

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
)

您的模板在静态 url 之前使用域:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2018-09-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2015-08-01
    • 2012-04-26
    相关资源
    最近更新 更多