Python-电子邮件中有多个嵌入图像-第一个是嵌入的,其余是附加的,为什么?
我有以下代码。但不知何故,只有第一个图像被嵌入。其余的图像将附加到电子邮件中。此外,不显示纯文本 你能帮我吗Python-电子邮件中有多个嵌入图像-第一个是嵌入的,其余是附加的,为什么?,python,email,Python,Email,我有以下代码。但不知何故,只有第一个图像被嵌入。其余的图像将附加到电子邮件中。此外,不显示纯文本 你能帮我吗 def create_msg2(img_list): msgRoot = MIMEMultipart('related') # Set the email subject. msgRoot['Subject'] = 'This email contain both Html, text and images.' # Set the email from e
def create_msg2(img_list):
msgRoot = MIMEMultipart('related')
# Set the email subject.
msgRoot['Subject'] = 'This email contain both Html, text and images.'
# Set the email from email address.
msgRoot['From'] = USER
# Set the email to email address.
msgRoot['To'] = TO
# Set the multipart email preamble attribute value. Please refer https://docs.python.org/3/library/email.message.html to learn more.
msgRoot.preamble = '====================================================='
# Create a 'alternative' MIMEMultipart object. We will use this object to save plain text format content.
msgAlternative = MIMEMultipart('alternative')
# Attach the bove object to the root email message.
msgRoot.attach(msgAlternative)
# Create a MIMEText object, this object contains the plain text content.
msgText = MIMEText('This object contains the plain text content of this email.')
# Attach the MIMEText object to the msgAlternative object.
msgAlternative.attach(msgText)
for img in img_list:
image_cid = make_msgid()
# Create a MIMEText object to contains the email Html content. There is also an image in the Html content. The image cid is image1.
msgText = MIMEText('<br><img src="cid:{image_cid}"></br>'.format(image_cid=image_cid[1:-1]), 'html')
# Attach the above html content MIMEText object to the msgAlternative object.
msgAlternative.attach(msgText)
# Open a file object to read the image file, the image file is located in the file path it provide.
with open(img, 'rb') as f:
# Create a MIMEImage object with the above file object.
msgImage = MIMEImage(f.read())
# Add 'Content-ID' header value to the above MIMEImage object to make it refer to the image source (src="cid:image1") in the Html content.
msgImage.add_header('Content-ID', image_cid)
# Attach the MIMEImage object to the email body.
msgRoot.attach(msgImage)
return msgRoot
def create_msg2(img_列表):
msgRoot=MIMEMultipart('related')
#设置电子邮件主题。
msgRoot['Subject']=“此电子邮件包含Html、文本和图像。”
#设置来自电子邮件地址的电子邮件。
msgRoot['From']=用户
#将电子邮件设置为电子邮件地址。
msgRoot['To']=To
#设置多部分电子邮件前言属性值。请参阅https://docs.python.org/3/library/email.message.html 了解更多。
msgRoot.preamble='=========================================================================================================
#创建“可选”MIMEMultipart对象。我们将使用此对象保存纯文本格式的内容。
msgAlternative=MIMEMultipart('备选方案')
#将上述对象附加到根电子邮件。
msgRoot.attach(msgaltentive)
#创建一个MIMEText对象,该对象包含纯文本内容。
msgText=MIMEText('此对象包含此电子邮件的纯文本内容')
#将MIMEText对象附加到msgaltentive对象。
msgAlternative.attach(msgText)
对于img_列表中的img:
image_cid=make_msgid()
#创建一个MIMEText对象以包含电子邮件Html内容。Html内容中还有一个图像。图像cid是图像1。
msgText=MIMEText(“
”。格式(image\u cid=image\u cid[1:-1]),“html”)
#将上述html内容MIMEText对象附加到msgaltentive对象。
msgAlternative.attach(msgText)
#打开文件对象以读取图像文件,图像文件位于它提供的文件路径中。
将open(img,'rb')作为f:
#使用上面的文件对象创建一个MIMIMAGE对象。
msgImage=MIMEImage(f.read())
#将“Content ID”头值添加到上述MIMEImage对象中,使其引用Html内容中的图像源(src=“cid:image1”)。
msgImage.add_头('Content-ID',image_-cid)
#将MIMEImage对象附加到电子邮件正文。
msgRoot.attach(msgImage)
返回msgRoot
好的,现在可以了。关键是只附加一次html字符串
def create_msg(img_list):
msgRoot = MIMEMultipart('related')
today = datetime.today().strftime('%Y-%m-%d')
msgRoot['Subject'] = f'Schlagzeilen, {today}'
msgRoot['From'] = USER
msgRoot['To'] = TO
# Set the multipart email preamble attribute value. Please refer https://docs.python.org/3/library/email.message.html to learn more.
msgRoot.preamble = '====================================================='
# Create a 'alternative' MIMEMultipart object. We will use this object to save plain text format content.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = '<html><body><h1>Schlagzeilen</h1><br>'
for img in img_list:
image_cid = make_msgid()
msgText += '<img src="cid:{image_cid}"><br>'.format(image_cid=image_cid[1:-1])
# Open a file object to read the image file
with open(img, 'rb') as f:
msgImage = MIMEImage(f.read())
# Add 'Content-ID' header value to the above MIMEImage object to make it refer to the image source in the Html content.
msgImage.add_header('Content-ID', image_cid)
msgRoot.attach(msgImage)
msgText += '</body></html>'
msgAlternative.attach(MIMEText(msgText, 'html'))
return msgRoot
def create_msg(img_列表):
msgRoot=MIMEMultipart('related')
today=datetime.today().strftime(“%Y-%m-%d”)
msgRoot['Subject']=f'Schlagzeilen,{today}
msgRoot['From']=用户
msgRoot['To']=To
#设置多部分电子邮件前言属性值。请参阅https://docs.python.org/3/library/email.message.html 了解更多。
msgRoot.preamble='=========================================================================================================
#创建“可选”MIMEMultipart对象。我们将使用此对象保存纯文本格式的内容。
msgAlternative=MIMEMultipart('备选方案')
msgRoot.attach(msgaltentive)
msgText='Schlagzeilen
'
对于img_列表中的img:
image_cid=make_msgid()
msgText+='
'.格式(image\u cid=image\u cid[1:-1])
#打开文件对象以读取图像文件
将open(img,'rb')作为f:
msgImage=MIMEImage(f.read())
#将“Content ID”头值添加到上面的MIMEImage对象中,使其引用Html内容中的图像源。
msgImage.add_头('Content-ID',image_-cid)
msgRoot.attach(msgImage)
msgText+=“”
附加(MIMEText(msgText,'html'))
返回msgRoot