6

I have looked at several other posts including:

Embed picture in email

Sending Multipart html emails which contain embedded images

creating a MIME email template with images to send with python / django

These along with the python docs for smtplib and email have gotten me close. I am using the code below to create an email with a simple jpg embedded in it. If I send the email to gmail it will display the embedded image fine but outlook 2013 will not.

import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

From = ''
To = ''

msg = MIMEMultipart()
msg['Subject'] = 'image test message'
msg['From'] = From
msg['To'] = To


text = 'This is sample text from me'
html = '''
<html>
    <head>
        <title> this is a test title </title>
    </head>
    <body>
        <p> Test me <br>
        Another line <br>
        This is the image you were looking for <img src="cid:test_image"><br>
        This will teach you not to click on links in strange <a href="http://purple.com">emails</a>
        </p>
    </body>
</html>
'''

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

img_data = open('image.jpg', 'rb').read()
img = MIMEImage(img_data, 'jpeg')
img.add_header('Content-Id', '<test_image>')
msg.attach(img)

s = smtplib.SMTP('localhost')
s.sendmail(From, To, msg.as_string())
s.quit()

I have checked all the download and security settings in Outlook that I can think of and they are fine. I have also added the sender to the safe-sender's list. I am able to receive other emails created with embedded images using conventional tools in outlook fine. From the reading I have been doing and looking at the source of the received email it appears that outlook doesn't know where to find the image. There are no attachments associated with this email either. Below is what I get when I right-click on the email and view the source.

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"><title> this is a test title </title>
    </head>
    <body>
        <p> Test me <br>
        Another line <br>
        This is the image you were looking for <img src="cid:test_image"><br>
        This will teach you not to click on links in strange <a href="http://purple.com">emails</a>
        </p>
    </body>
</html>

I am currently thinking it has something to do with either the content type or I just plain screwed up the code. I think the code is fine as gmail displays the image ok and when I forward it from gmail to outlook the forwarded message is displayed fine.

Community
  • 1
  • 1
Matty
  • 1,100
  • 1
  • 9
  • 13

1 Answers1

2

EDIT 2:

Try first without the simple text version:

Content-Type: multipart/related;
    boundary="----=_NextPart_000_0009_01CEC44B.4C788080"

When this shows the image then try the following and putting the elements into an alternative part like this:

Subject: ...
From: ...
To: ...
Content-Type: multipart/related;
    type="multipart/alternative";
    boundary="----=_NextPart_000_0009_01CEC44B.4C788080"

------=_NextPart_000_0009_01CEC44B.4C788080
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_000A_01CEC44B.4C788080"


------=_NextPart_001_000A_01CEC44B.4C788080
Content-Type: text/plain;
    charset="ISO-8859-15"
Content-Transfer-Encoding: quoted-printable

My Simple text

------=_NextPart_001_000A_01CEC44B.4C788080
Content-Type: text/html;
    charset="ISO-8859-15"
Content-Transfer-Encoding: quoted-printable

My HTML Text

------=_NextPart_001_000A_01CEC44B.4C788080--

------=_NextPart_000_0009_01CEC44B.4C788080
Content-Type: image/png;
    name="caddiigg.png"
Content-Transfer-Encoding: base64
Content-ID: <38F81D2D49CB42B2AD8F93F5CF01BCA1@SKNB>

iVBORw0KGgoAAAANSUhEUgAAAxcAAAH0CAIAAADADUduAAAgAElEQVR4nEy8adP02H3ex0+TFymZ
5JAzw01OpazZOVS2SjkvYpHzzELasiuOK4tLkhVJMcWZu7E0loN96x07cPZzsPR2P8+QlPOh8gL9
DFn1L


------=_NextPart_000_0009_01CEC44B.4C788080--

At the moment the sourcecode shows an email like this:

Content-Type: multipart/mixed; boundary="===============0661849094=="
MIME-Version: 1.0
Subject: image test message
From: 
To: 

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

This is sample text from me
--===============0661849094==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit


<html>
    <head>
        <title> this is a test title </title>
    </head>
    <body>
        <p> Test me <br>
        Another line <br>
        This is the image you were looking for <img src="cid:test_image"><br>
        This will teach you not to click on links in strange <a href="http://purple.com">emails</a>
        </p>
    </body>
</html>

--===============0661849094==
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Id: <test_image>

YmxhYmxh
--===============0661849094==--

EDIT 1:

This is what an Email looks like that works:

...
<BR><IMG alt=3D""=20
src=3D"cid:38F81D2D49CB42B2AD8F93F5CF01BCA1@SKNB">
...

------=_NextPart_000_0009_01CEC44B.4C788080
Content-Type: image/png;
    name="caddiigg.png"
Content-Transfer-Encoding: base64
Content-ID: <38F81D2D49CB42B2AD8F93F5CF01BCA1@SKNB>

iVBORw0KGgoAAAANSUhEUg ....

I see a difference: Content-ID - an uppercase D

User
  • 14,131
  • 2
  • 40
  • 59
  • I have seen what you posted above in other examples I am just not sure how to get there. The example I am going off of is in perl (which I don't know as well) and does a lot of base64 encoding and boundary generating by hand. I think I am on the right path only because gmail works fine, but my source looks nothing like what you posted. Thanks for the correction on Content-ID, I changed it previously but forgot to make the change in my post. – Matty Oct 08 '13 at 19:59
  • 1
    Thanks, removing the plain text part did the trick for me. For others finding this, the only changes I made to my above code were removing 'text' and 'part1', then commenting out the msg.attach part1. After that the image and html content displayed fine(which is all I really need) – Matty Oct 09 '13 at 02:45
  • i use this but not working ... https://docs.google.com/file/d/0Bx7i5tBqPWWWZUpMM3JVajRKdEk/edit?usp=drivesdk – Prashant Dec 23 '14 at 12:37