如何使用 exchangelib 库在电子邮件正文中嵌入图像 python

How to embed an image in the body of an email using exchangelib library python

我需要在电子邮件正文中嵌入多张图片。我试过下面的代码,但它没有将它嵌入到电子邮件的正文中。我不想把它作为附件

with open(i, 'rb') as f:
    my_logo = FileAttachment(
    name=i,
    content=f.read(),
    is_inline=True,
    content_type='GIF/Image',
    content_id=i,
)
m.attach(my_logo)

谢谢

查看 exchangelib 文档中的示例:https://ecederstrand.github.io/exchangelib/#attachments

除了创建附件外,您还需要在 HTML 正文中引用它:

message = Message(...)
logo_filename = 'logo.png'
with open(logo_filename, 'rb') as f:
    my_logo = FileAttachment(
        name=logo_filename, content=f.read(),
        is_inline=True, content_id=logo_filename,
    )
message.attach(my_logo)
message.body = HTMLBody(
    '<html><body>Hello logo: <img src="cid:%s"></body></html>' % logo_filename
    )