如何使用Python / Django在电子邮件中发送内联图像? [英] How can I send Inline images in Email with Python/Django?

查看:138
本文介绍了如何使用Python / Django在电子邮件中发送内联图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python / Django发送带有内联图片的电子邮件。

I'm trying to send an email with an inline image using Python/Django.

这里 是显示我如何做的代码。
它还在开发中。所以现在所有的意思是发送一个虚拟的电子邮件信息,其中嵌有一个大黄蜂的照片。

Here is the code showing how I am doing it. It's still in development. So all it is meant to do for now is send a dummy email message with a picture of a bumble bee embedded in it.

然而,当我收到我的Gmail中的电子邮件收件箱中,我只看到以下基于文本的电子邮件。邮件的各种Mime部分作为文本显示在电子邮件的有效内容中。
我点击了Gmail中的显示原始按钮,并将下面的整个电子邮件粘贴下来,以便您可以看到我所得到的。

Yet when I receive the email in my Gmail inbox, I see only the following text-based email. The various Mime parts of the email show up in the payload of the email as text. I clicked the "Show Original" button in Gmail and cut-n-pasted the entire email below so you can see what I get.

有人可以建议我在这里做错什么?还有一个可能的解决方案?

Can someone suggest what I'm doing wrong here? And a possible solution?

Delivered-To: myemail@gmail.com
Received: by 10.58.189.196 with SMTP id gk4csp207059vec;
        Mon, 17 Feb 2014 23:10:53 -0800 (PST)
X-Received: by 10.140.22.145 with SMTP id 17mr38512811qgn.0.1392707452834;
        Mon, 17 Feb 2014 23:10:52 -0800 (PST)
Return-Path: <0000014443d53bd9-c1021b39-b43e-4d6f-bb55-0aff6c4b38f5-000000@amazonses.com>
Received: from a8-41.smtp-out.amazonses.com (a8-41.smtp-out.amazonses.com. [54.240.8.41])
        by mx.google.com with ESMTP id j50si9661440qgf.137.2014.02.17.23.10.52
        for <myemail@gmail.com>;
        Mon, 17 Feb 2014 23:10:52 -0800 (PST)
Received-SPF: pass (google.com: domain of 0000014443d53bd9-c1021b39-b43e-4d6f-bb55-0aff6c4b38f5-000000@amazonses.com designates 54.240.8.41 as permitted sender) client-ip=54.240.8.41;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of 0000014443d53bd9-c1021b39-b43e-4d6f-bb55-0aff6c4b38f5-000000@amazonses.com designates 54.240.8.41 as permitted sender) smtp.mail=0000014443d53bd9-c1021b39-b43e-4d6f-bb55-0aff6c4b38f5-000000@amazonses.com
Return-Path: 0000014443d53bd9-c1021b39-b43e-4d6f-bb55-0aff6c4b38f5-000000@amazonses.com
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Hello World3
From: My Django App <donotrespond@mydjangoapp.com>
To: myemail@gmail.com
Date: Tue, 18 Feb 2014 07:10:51 +0000
Message-ID: <0000014443d53bd9-c1021b39-b43e-4d6f-bb55-0aff6c4b38f5-000000@email.amazonses.com>
X-SES-Outgoing: 2014.02.18-54.240.8.41

Content-Type: multipart/related;
 boundary="===============1003274537458441237=="
MIME-Version: 1.0

--===============1003274537458441237==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<p>Hello <img src="cid:myimage" /></p>
--===============1003274537458441237==
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Id: <myimage>

/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQTERUUEhIWFBUVFxcVFRQVGBUUFRcYFxUWFhQU
FRUYHCggGRolHRQVITEiJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGhAQGywmICYzLDc3MCwvLCw1

<VERY LARGE PORTION SNIPPED>

BAgQIECAAIGaAsLKmnPVFQECBAgQIECAAAECBAgQIECAAIF0AsLKdCNTMAECBAgQIECAAAECBAgQ
IECAAIGaAsLKmnPVFQECBAgQIECAAAECBAgQIECAAIF0Av8HNFl0J1BnG68AAAAASUVORK5CYII=
--===============5170682983005376168==--


推荐答案

看起来像你有:

multipart/related
-> text/html
-> image/jpeg

以前发送邮件时遇到麻烦,顶部是多部分/有关。尝试这样做:

I've also had trouble in the past sending email with the top part being multipart/related. Try this instead:

multipart/mixed
-> multipart/related
--> text/html
--> image/jpeg

另外,请确保并设置图像上的处理方式:

Also, make sure and set the disposition on the image like this:

img.add_header("Content-Disposition", "inline", filename="myimage")

这篇关于如何使用Python / Django在电子邮件中发送内联图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆