1
from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from datetime import datetime
import socket
import email
import smtplib
import uuid

class EmailSender:
def __init__(self):
    self.msgRoot = MIMEMultipart('related')
    self.msgAlternative = MIMEMultipart('alternative')
    self.msgRoot.attach(self.msgAlternative)

def AddImage(self, fileName, title):
    internalFileName = '%s-%s-%s' %(fileName, datetime.now().strftime('%Y%m%d%H%M%S'), uuid.uuid4())
    mimetext ='<p style="background-color:lightgrey;font-size:20px;font-weight:bold;font-family:Comic Sans MS">%s</p><br><img src="cid:%s"><br>' %(title, internalFileName)
    msgText = MIMEText(mimetext, 'html')
    self.msgAlternative.attach(msgText)
    fp = open(fileName, 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    msgImage.add_header('Content-ID', '<%s>' %(internalFileName))
    self.msgRoot.attach(msgImage)

def AddFile(self, fileName):
    fg = open(fileName, 'rb')
    fA = MIMEApplication(fg.read(), Name=fileName)
    fA['Content-Disposition'] = 'attachment; filename="%s"' %fileName
    self.msgRoot.attach(fA)

This is my code of adding a image to the result email AddImage(). But when I invoke AddImage() for more than once, only the first image is shown in the result email?

Can you help find the problem?

3 Answers 3

2

The solution turns out to be:

def AddImage(self, fileName, title):
    internalFileName = '%s-%s-%s' %(fileName, datetime.now().strftime('%Y%m%d%H%M%S'), uuid.uuid4())
    self.imgHtml +='<p style="font-size:15px;font-weight:bold;font-family:Comic Sans MS">%s</p><br><img src="cid:%s"><br>' %(title, internalFileName)
    fp = open(fileName, 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    msgImage.add_header('Content-ID', '<%s>' %(internalFileName))
    self.msgRoot.attach(msgImage)
def Send(self, toList):
    msgText = MIMEText(self.imgHtml, 'html')
    self.msgAlternative.attach(msgText)
    self.msgRoot['Subject'] = 'Audience Ingestion Integrated Test Report @%s [%s]' %(datetime.now().strftime('%Y-%m-%d'), socket.gethostname())
    strFrom = '[email protected]'
    self.msgRoot['From'] = strFrom
    strTo = email.Utils.COMMASPACE.join(toList)
    self.msgRoot['To'] = strTo
    smtp = smtplib.SMTP('smtp1.dev.fwmrm.net', 25)
    smtp.sendmail(strFrom, strTo, self.msgRoot.as_string())
    smtp.quit()

which means that when AddImage(), just attach the MIMEImage to the MIMEMultipart and add the string to the html string, and when Send() after several invocations of AddImage(), attach the MIMEText generated from the html string to MIMEMultipart.

Sign up to request clarification or add additional context in comments.

Comments

2

This code works but i am also trying to add text with each image but idk how to do that :

home = str(Path.home())
path = home + "\\Pictures"
list_of_images = glob( os.path.join(path, "*.png") )

mail = MIMEMultipart("related")
#Jinja2 for html template

main = Template('''
    <html><body> 
    <h2>This is a Test email for python script</h2>
    <br />
    {% for image in pictures %}<p> $(name) </p><br /><img src="cid:{{image}}"><br />{% endfor %}
    </body></html>''')



for filename in list_of_images:
    fp = open(filename, 'rb')
    msg_img = MIMEImage(fp.read())
    fp.close()
    msg_img.add_header('Content-ID', '<{}>'.format(filename))
    msg_img.add_header('Content-Disposition', 'inline', filename=filename)
    mail.attach(msg_img)

mail['Subject'] = "Python Script Test | You will receive this email every 30 minutes"
mail['From'] = me
mail['To'] = you

Comments

0

This was helpful for me.

What I found out that helped, after I have included (looped over) all the <img src="cid:%s"> use msgAlternative.as_string() and that fixed the issue. The issue being how it only included the first image now matter how many images I included.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.