简体   繁体   English

Django + Anymail + Mailgun-电子邮件HTML呈现没有按钮和图像

[英]Django + Anymail + Mailgun - Email HTML renders without button and image

I am having an issue while trying to send an email containing html and an image, through mailgun, using the anymail library. 尝试使用anymail库通过mailgun发送包含html和图像的电子邮件时遇到问题。

This is my code: 这是我的代码:

url_formulario = CLIENT_URL + str(token.key)
email = EmailMultiAlternatives('Confirmación Vacante', to=emails)
cid = attach_inline_image_file(email, '/var/www/static/icons/ba_logo.png')
contexto = {'nombre_contacto': contacto.responsable_nombre, 
            'nombre_alumno': contacto.alumno_nombre, 
            'url_formulario': url_formulario,
            'imagen':cid}
mensaje = render_to_string('email.html', context=contexto)
email.attach_alternative(mensaje, "text/html")
email.track_clicks = True
email.send()

I have also tried doing it like this: 我也尝试过这样做:

url_formulario = CLIENT_URL + str(token.key)
contexto = {'nombre_contacto': contacto.responsable_nombre, 
            'nombre_alumno': contacto.alumno_nombre, 
            'url_formulario': url_formulario}
mensaje = render_to_string('email.html', context=contexto)
content = strip_tags(mensaje)
email = EmailMultiAlternatives('Confirmación Vacante', content,to=emails)
email.attach_alternative(mensaje, "text/html")
email.track_clicks = True
email.send()

Here are the two corresponding versions of the html file I am using: 这是我正在使用的html文件的两个相应版本:

<html>
<head>
    <title>Ingresa al formulario</title>
        <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Bastrap CSS -->
    <link rel="stylesheet" href="/static/css/bastrap.css">      
    <style>
            .contenedor-general{
                background:#e5e5e5;
                padding-top:3em;
            }
            .contenedor-general img{
                padding-bottom:3em;
            }
            .contenido-mensaje{
                background:white;
                margin-bottom:calc(43px + 6em);
            }
            .contenido-mensaje p{
                font-family:"CHANEWEI", Helvetica, Arial, sans-serif;
                margin:7%;
                color:#717170;
            }
            .contenido-mensaje h1,
            .contenido-mensaje a{
                margin: 0 7% 0 7%;
            }
            .contenido-mensaje h1{
                padding-top:7%;
                color:#717170;
            }
            .contenido-mensaje a{
                color:#333;
            }
            .btn-primary{
                background-color:#fcda59 !important;
                color:#685723 !important;
                box-shadow:none !important;
            }

            .btn-primary:hover{
                background-color:#fdd306 !important;
                border-color:#fdd306 !important;
                color:#685723 !important;
                box-shadow:none !important;
            }
    </style>
</head>
<body>
    <div class="container">
        <div class="contenedor-general col-lg-8 col-lg-offset-2">
            <img src="{{imagen}}" alt="Logo Buenos Aires" class="center-block"/>
            <div class="col-lg-8 col-lg-offset-2 contenido-mensaje">
                <h2>Hola {{nombre_contacto}},</h2>
                <p>Tenemos una vacante escolar pendiente para {{nombre_alumno}}</p>
                <a href='{{url_formulario}}' class="btn btn-lg btn-primary">Confirmar vacante</a>
                <p>Si tenés problemas para ingresar comunicate al XXXX-XXXX (Número de télefono)</p>
                <p>Muchas gracias</p>
            </div>
    </div>
    </body>
</html>

Another version of the tag without passing the image: 标签的另一个版本,但未传递图片:

<img src="" alt="Logo Buenos Aires" class="center-block"/>

This is the resulting email: 这是生成的电子邮件:

在此处输入图片说明

Is there a way to attach an html file after rendering it to a string with an specified context and an image attached? 在将HTML文件呈现为具有指定上下文和图像的字符串后,是否可以附加html文件?

Thanks. 谢谢。

It looks like your template is missing the cid: part of the <img src> . 看来您的模板缺少cid: <img src> You have: 你有:

<img src="{{imagen}}" alt="Logo Buenos Aires" class="center-block"/>

But that would need to be: 但这需要是:

<img src="cid:{{imagen}}" alt="Logo Buenos Aires" class="center-block"/>

The cid: scheme is how the email client knows to treat the value of {{imagen}} as an inline Content-ID. cid:方案是电子邮件客户端知道如何将{{imagen}}的值视为嵌入式Content-ID的方式。 Without it, the client doesn't know where it's supposed to look for that image source, so you get a broken image icon instead. 没有它,客户端将不知道应该在哪里寻找该图像源,因此,您会得到一个损坏的图像图标。

There's a little more detail in the Anymail docs Anymail文档中有更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM