python含多个附件的邮件_Python发送带有多个图像附件的电子邮件
我試圖用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()
總結
以上是生活随笔為你收集整理的python含多个附件的邮件_Python发送带有多个图像附件的电子邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 钢板桩多少钱一吨啊?
- 下一篇: python字典与json转换_pyth