【发布时间】:2022-01-16 03:21:51
【问题描述】:
我是 Python 的新手。我想将带有嵌入在左上角的公司徽标的基于 html 的电子邮件发送到电子邮件正文。
使用以下代码,电子邮件绝对可以正常工作,但不再附加嵌入的图像。不知道我在哪里做错了。谁能帮帮我。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart('alternative')
msg['Subject'] = "My text dated %s" % (today)
msg['From'] = sender
msg['To'] = receiver
html = """\
<html>
<head></head>
<body>
<img src="cid:image1" alt="Logo" style="width:250px;height:50px;"><br>
<p><h4 style="font-size:15px;">Some Text.</h4></p>
</body>
</html>
"""
# The image file is in the same directory as the script
fp = open('logo.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)
part2 = MIMEText(html, 'html')
msg.attach(part2)
mailsrv = smtplib.SMTP('localhost')
mailsrv.sendmail(sender, receiver, msg.as_string())
mailsrv.quit()
【问题讨论】: