1

I have this python script which sends emails using smtplib module.

#! /usr/bin/python

import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

from1 = "root"
to = "ankur.kulshrestha@ericsson.com"
subject = "test mail"

msg = MIMEMultipart()
msg['From'] = from1
msg['To'] = to
msg['Subject'] = subject

text = """Hi,
This is test messgae"""

html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

fi = open("dragon.jpg", 'rb')
img = MIMEImage(fi.read())
fi.close()
msg.attach(img)

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)

smtp_obj = smtplib.SMTP('rinacac-test.egi.ericsson.com')
smtp_obj.sendmail(from1, to, msg.as_string())
smtp_obj.quit()

Script is working but my html content 'html' & image 'dragon.jpg' are appearing as attachments. I want them to be displayed as the content of my mail not as attachments. Please help

Update: I am still not able to understand what role is Multipart/alternative is playing here..Moreover within the earlier link you have shared below line is not getting displayed in mail.

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

Secondly object 'msgText' which contains the html string is attached with 'msgAlnernative'

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
msgAlnernative.attach(msgText)

but object 'msgImage' which reads the image is attached with 'msgRoot'. Why it is so..

msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

I couldn't find any helping document on MIMEMultipart/related and alternative. Please help

Geetika
  • 47
  • 3
  • 8
  • #! /usr/bin/perl shouldn't it be /usr/bin/python? And you could probalby decode the image as base64 string and put it in an tag – BigZ Apr 20 '16 at 10:48
  • oops.. I mostly work on perl ;).. Anyways I was running the script like 'python script.py'.. :) – Geetika Apr 20 '16 at 10:50
  • 3
    may help you http://stackoverflow.com/questions/920910/sending-multipart-html-emails-which-contain-embedded-image – Rahul K P Apr 20 '16 at 10:52
  • Thanks Rahul.. Its working..but I am not able to understand this 'related' & 'alternative' arguments.. Could you please explain how they were used? – Geetika Apr 20 '16 at 11:36
  • http://stackoverflow.com/questions/11995720/python-emailing-multipart-with-body-content check this. If you need more let me know. – Rahul K P Apr 20 '16 at 13:18
  • Thanks Rahul for your time & effort.. I have updated my further queries in original mail. Please help. – Geetika Apr 21 '16 at 07:08

1 Answers1

1

I think this will solve your problem How to display base64 images in html

If your mailprovider supports html tags in emails

Sorry, as this is a specific python question and the examples in the link seems to be php here a python solution

import base64
img = 'test.jpg'
with open(img, 'rb') as imgfile:
    img64 = base64.encodestring(imgfile.read())

###

html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
    <img width="160" height="160" alt="tick" src="data:image/jpg;base64,{0}">
  </body>
</html>
""".format(img64)

###
Community
  • 1
  • 1
BigZ
  • 836
  • 8
  • 19