我正在尝试使用smtplib在Python中同时发送带有图像和数据帧的电子邮件。
我设法发送了图片或表格。但是当我将它们放在一起时,只会显示图像。
我遵循了本教程:Sending Multipart html emails which contain embedded images
仅显示图像,不显示任何表格,文本。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
msgRoot = MIMEMultipart('related')
msgRoot['From'] = gmail_user
msgRoot['To'] =','.join(to)
msgRoot['Subject'] = 'Test Message'
# Create the body of the message.
html = df1.loc[51:51][['source','created_at','text','processed_text']].to_html()+"""
<body>
<img src="cid:image1">
</body>
"""
# Record the MIME types.
msgHtml = MIMEText(html, 'html')
img = open('whatever.png', 'rb').read()
msgImg = MIMEImage(img, 'png')
msgImg.add_header('Content-ID', '<image1>')
msgImg.add_header('Content-Disposition', 'inline', filename='whatever.png')
msgRoot.attach(msgHtml)
msgRoot.attach(msgImg)
#send email...
我希望电子邮件中包含表格和图片。在这种情况下,请说明应使用哪种格式将图片放入html中。 谢谢!