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