python-3.x - 使用 Python Win32 发送电子邮件。将图像添加到电子邮件正文不起作用

标签 python-3.x

我正在尝试将图像添加到电子邮件正文。

我正在使用 Outlook 16 和 Python 3.7 来执行此操作。

邮件从邮箱发送。

这是我发送电子邮件的代码功能以及如何在电子邮件末尾添加图像。

def send_email(sender,recipient):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = Subject_Req
    mail.HTMLBody = Content_Email
    mail.SentOnBehalfOfName = sender
    mail.GetInspector 
    mail.Send()

该图像存在于我的本地网络中:- C:\Users\Sid\AppData\Roaming\Microsoft\Signatures\我的项目\image001.png

图像不过是我想放在 HTML 正文最后部分的 Logo 。

所以基本上从函数来看,它将是 Content_Email + This image。

我尝试了一些代码,它的作用是:- 它在末尾发送一个“X”标记来代替图像本身给收件人。

当我将其发送给自己时,它会打上“X”标记,但我可以选择右键单击下载图片并获得图像。

我想做的是,在这两种情况下都放置图像而不是“X”,而用户不必访问该图像。

如何使用 Python 做到这一点。这里的解决方案似乎与 VBA 一起使用:https://www.experts-exchange.com/questions/26481413/Problem-inserting-HTML-images-into-the-body-of-a-messge.html

最佳答案

我设法通过 解决了这个问题附件.PropertyAccessor.SetProperty 功能。

def send_email(sender,recipient):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = Subject_Req
    attachment = mail.Attachments.Add(signatureimage)
    attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
    mail.HTMLBody = Content_Email + "<html><body><img src=""cid:MyId1""></body></html>"
    mail.SentOnBehalfOfName = sender
    mail.GetInspector 
    mail.Send()

#Change the Paths here, if ran from a different location
signatureimage = "C:\\Users\\Sid\\AppData\\Roaming\\Microsoft\\Signatures\\My Project\\image001.png"

这意味着,它将嵌入图像而不是将图片链接到链接。链接到图片需要收件人有权访问该特定图片位置。

此外,对于学习,此链接根本不会改变:- http://schemas.microsoft.com/mapi/proptag/0x3712001F

关于python-3.x - 使用 Python Win32 发送电子邮件。将图像添加到电子邮件正文不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57432957/

相关文章:

python - 从显示中删除 matplotlib 弃用警告

python-3.x - Python 占用的内存是应有的六倍

Python 3 - 如何更新字典以在一个键中包含多个值?

python - 删除核密度图中的背景颜色 (seaborn.kdeplot)

python - Python 猜数字中的重新启动选项

Python Flask 将访问日志写入 STDERR

python - 在两个日期之间合并大型 Pandas 数据框的有效方法

python - 如何禁用 OptionMenu 中的单个选项?

python - 如何检查要完成下载 Python3 的文件?

Python write 函数不保存所有图像