使用python脚本发送包含多个嵌入图像的html电子邮件

2024-10-03 19:30:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的新手。我想发送带有多个嵌入图像的基于html的电子邮件

使用下面的代码,电子邮件是绝对有效的,但只能查看一个嵌入的图像和另一个未定义的图像

def addMultipleEmbeddedImages(self, nbMsg=1):

        attachedFiles = ["giga_logo_300x225.png", "phone_logo.jpg"]

        self.imgHtml = ""

        for file1 in attachedFiles:
            file = os.path.join(pathToAttachments, file1)                       

            self.__msg = MIMEMultipart('related')

            self.imgHtml +='<p <u>Embedded Images</u></p><br><img src="cid:%s"><br>'%file     

            self.__msg.attach(MIMEText(self.imgHtml, 'html'))

            fp = open(file, 'rb')
            msgImage = MIMEImage(fp.read())
            fp.close()

            msgImage.add_header('Content-ID', '<%s>'%file)
            self.__msg.attach(msgImage)            

        self.__fillInAddressFields()

        # Attach the email in the body
        self.__buildBody(self.__msg)

        # Send the email with the embedded image
        self.__sendMessages(nbMsg)

        return

Tags: thein图像self电子邮件htmlmsgfile1