1

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.

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==--
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • There is no answer in the original posting. The 5 people who marked this as a "Duplicate" with an answer somewhere else are incorrect. – Saqib Ali Feb 18 '14 at 23:12

1 Answers1

1

It looks like you have:

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")
David K. Hess
  • 16,632
  • 2
  • 49
  • 73
  • As you can see, I followed these instructions nearly exactly: http://stackoverflow.com/questions/1633109/creating-a-mime-email-template-with-images-to-send-with-python-django Do you know why the instructions didn't work for me? – Saqib Ali Feb 19 '14 at 00:09
  • 1
    Every email program has its own idiosyncratic way of dealing with email. What I put in my answer is I think closest to what is pretty standard. – David K. Hess Feb 19 '14 at 03:24