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?