Python:发送SMTP HTML电子邮件,但未加载图片

Python:发送SMTP HTML电子邮件,但未加载图片,python,html,email,Python,Html,Email,我正在尝试从我的python应用程序发送电子邮件。 我有一个代码发送SMTP电子邮件与HTML代码包括在内。我在txt文件中有我的HTML代码,如果我在localhost上作为一个页面启动它,所有的图片都会显示出来。而且我所有的文件都在同一个目录下。我需要的格式如下: 这一行是HTML中导入的图像之一 <!--[if mso]><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr s

我正在尝试从我的python应用程序发送电子邮件。 我有一个代码发送SMTP电子邮件与HTML代码包括在内。我在txt文件中有我的HTML代码,如果我在localhost上作为一个页面启动它,所有的图片都会显示出来。而且我所有的文件都在同一个目录下。我需要的格式如下:

这一行是HTML中导入的图像之一

<!--[if mso]><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr style="line-height:0px"><td style="padding-right: 0px;padding-left: 0px;" align="center"><![endif]--><img align="center" alt="server" border="0" class="center fixedwidth" src="server.png" style="text-decoration: none; -ms-interpolation-mode: bicubic; border: 0; height: auto; width: 100%; max-width: 177px; display: block;" title="Image" width="177"/>

with open('messages.txt', mode = 'r' , encoding= 'UTF-8') as oap:
        z = oap.read()
    time.sleep(3)
    message.add_alternative(f"{z}", subtype = 'html')

将open('messages.txt',mode='r',encoding='UTF-8')作为oap:
z=oap.read()
时间。睡眠(3)
message.add_alternative(f“{z}”,子类型='html')
使用此代码,我打开并读取文件,完成后通过SMTP发送

我还试着把照片的位置改成这个- C:\Users\DzITC\Desktop\parking app\server.png

在web中更改图像后,它会加载图像,但在python中不会


在电子邮件中使用文件系统路径作为img的src是行不通的。包含图像的一种方法是将内容id指定为src:
。内容id必须符合中的规范:Python的电子邮件包可以生成一致的id

from email.message import EmailMessage
from email.utils import make_msgid


html = """
<!-- example markup - add missing html, head, body etc tags -->
<p>Hello</p>
<!-- Use a format string place holder for the content id --> 
<img alt="My image" src="cid:{image_cid}"/>
"""

# Create a message
msg = EmailMessage()

# Create a content id for the image
image_cid = make_msgid()

# Add the html to the message, replacing the placeholder with the image id
# (the '<' and '>' characters at the start and end of the id must be stripped)
msg.add_alternative(html.format(image_cid=image_cid[1:-1], subtype='html'))

# Add the image to the message
with open('myimage.png', 'rb') as f:
    img = f.read()
    msg.add_related(img, 'image', 'png', cid=image_cid)
从email.message导入EmailMessage
从email.utils导入make_msgid
html=”“”
你好

""" #创建消息 msg=EmailMessage() #为图像创建内容id image_cid=make_msgid() #将html添加到消息中,用图像id替换占位符 #(必须删除id开头和结尾的“”字符) msg.add\u alternative(html.format(image\u cid=image\u cid[1:-1],subtype='html')) #将图像添加到消息中 将open('myimage.png','rb')作为f: img=f.read() msg.add_-related(img,'image','png',cid=image_-cid)
您是否尝试过检查您发送的邮件。我在php中遇到类似问题,因为谷歌在邮件中给出的图像链接之前添加了自己的内容。您的意思是什么?当您检查邮件中的图像时,是否看到与您在img标记的src属性中给出的路径相同的路径?不,我没有看到,我上载了屏幕截图。make_msg()应该是make_msgid()