简体   繁体   English

使用 Django 在 HTML 电子邮件模板中将图像作为内嵌附件发送

[英]Sending Images as inline Attachment within HTML Email template using Django

I am trying to send an HTML email that would render an inline image.我正在尝试发送将呈现内嵌图像的 HTML 电子邮件。 My code in views.py is as follows:我在views.py中的代码如下:

import ...

def signup_mail_function(form):      # here, form is just my context 
    subject = "Signup Successful"
    html_message = render_to_string('emailTemplate.html', {'form': form})
    plain_message = strip_tags(html_message)
    from_email = 'uconnect786@gmail.com'
    to_email = [form.email]

    # Sending email here
    msg = EmailMultiAlternatives(subject=subject, body=plain_message, from_email=from_email,to=to_email)                               
    msg.attach_alternative(html_message, "text/html")
    msg.content_subtype = 'html'
    msg.mixed_subtype = 'related'

    img_path = settings.STATIC_DIR + '/images/blogsImage.svg'  # path of my image
    image_name = Path(img_path).name      # Testing image name,which returns out to be same as image    name i.e blogsImage.svg
    print(img_path, image_name)                              # testing image paths here
    with open(img_path, 'rb') as f:
        image = MIMEImage(f.read(), _subtype="svg+xml")
        msg.attach(image)
        image.add_header('Content-ID', "<{}>".format(image_name))    # Setting content ID
        print("<{}>".format(image_name))                       # Testing content ID

    msg.send()

So the above is my handling code of email message.所以上面是我处理电子邮件的代码。 In the template, I am doing the following:在模板中,我正在执行以下操作:

<img  alt="Blog Image"  src="cid:blogsImage.svg" />

Email is sent successfully with the image as the attachment but actually I was expecting to get the image as an inline image within the Html page.电子邮件以图像作为附件成功发送,但实际上我希望将图像作为 Html 页面中的内嵌图像。

The email I received, You see no images are displayed within Html template so 'alt' tags of those images pop up我收到的电子邮件,您看到 Html 模板中没有显示任何图像,因此弹出这些图像的“alt”标签

在此处输入图片说明

I could snapshot the whole Html page but below it, I have that image as an attachment.我可以对整个 Html 页面进行快照,但在其下方,我将该图像作为附件。

Tried:尝试:

I also tried to test image insertion within my Html template using direct URLs like the following:我还尝试使用如下所示的直接 URL 在我的 Html 模板中测试图像插入:

<img src="https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=.........." alt="Image">

But that didn't work out either.但这也没有奏效。 I have almost read every article and blog about rendering inline images with Django, stack overflow questions and answers but none of them has worked for me although I am coding the same way.我几乎阅读了所有关于使用 Django 渲染内联图像、堆栈溢出问题和答案的文章和博客,但尽管我以相同的方式编码,但它们都没有对我有用。 Kindly someone point me in the right direction here!请有人在这里指出我正确的方向!

first u should import following code in view.py首先你应该在view.py中导入以下代码

from email.mime.image import MIMEImage import os从 email.mime.image 导入 MIMEImage 导入 o​​s

def signup_mail_function(form):
msg = EmailMultiAlternatives(subject=subject, body=plain_message, 
from_email=from_email,to=to_email)
msg.content_subtype = 'html'
msg.mixed_subtype = 'related'
img_path = os.path.join(settings.BASE_DIR, '/images/blogsImage.svg')
with open(banner_path, 'rb') as banner_image:
    banner_image = MIMEImage(img_path.read())
    banner_image.add_header('Content-ID', '<blogsImage.svg>')
    msg.attach(banner_image)
msg.send()

and in html templates并在 html 模板中

 <img alt="Blog Image" src="cid:blogsImage.svg" />

I found the answer to my question...!我找到了我的问题的答案......! The problem was not in the code as I was expecting, but in the image, I was trying to send an 'SVG' image in my email message which is not supported by many email clients even Gmail so that was the problem.问题并不在我所期望的代码中,而是在图像中,我试图在我的电子邮件中发送一个“SVG”图像,许多电子邮件客户端甚至 Gmail 都不支持它,所以这就是问题所在。 The code is working fine with other formats.该代码在其他格式下运行良好。 You can get more about supported email formats here:您可以在此处获取有关支持的电子邮件格式的更多信息:

How can I embed SVG into HTML in an email, so that it's visible in most/all email browsers? 如何将 SVG 嵌入到电子邮件的 HTML 中,以便它在大多数/所有电子邮件浏览器中可见?

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

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