我试图用Python发送一封带有多个图像附件的电子邮件。但是通过下面的代码,我可以在正文中包含第一个图像,但是第二个图像会作为附件附加到电子邮件中。有没有办法可以在HTML的主体中同时获得这两个图像?下面是我当前的代码。在from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
strFrom = 'user@provider.com'
strTo = 'user@provider.com'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Test message'
msgRoot['From'] = 'user@provider.com'
msgRoot['To'] = 'user@provider.com'
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText('Test HTML with Images
'
''
'
'
'
'
''
'
'
'
'
'Sending Two Attachments', 'html')
msgAlternative.attach(msgText)
fp = open('image1.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage)
fp = open('image2.png', 'rb')
msgImage2 = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage2)
import smtplib
smtp = smtplib.SMTP('localhost')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()