如何使用Python / Django在电子邮件中发送内联图像?

时间:2014-02-18 17:51:24

标签: python django email

我尝试使用Python / Django发送包含内嵌图片的电子邮件。

Here是显示我如何做的代码。 它还在开发中。所以现在要做的就是发送一个虚拟的电子邮件,里面嵌着一只大黄蜂的照片。

然而,当我在Gmail收件箱中收到电子邮件时,我只会看到以下基于文字的电子邮件。电子邮件的各种Mime部分显示在电子邮件的有效负载中作为文本。 我点击了" Show Original" Gmail中的按钮,然后剪切粘贴下面的整个电子邮件,以便您可以看到我得到的内容。

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

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==--

1 个答案:

答案 0 :(得分:1)

看起来你有:

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

我在过去发送电子邮件时遇到了麻烦,其中顶部是多部分/相关的。试试这个:

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

另外,请确保并在图像上设置处置,如下所示:

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