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

MMS*_*MMS 5 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()
Run Code Online (Sandbox Code Playgroud)

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

图像只不过是我想放在 HTML 正文最后一部分的徽标。

所以基本上从函数来看,它将是 Content_Email + 此图像。

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

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

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

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

MMS*_*MMS 5

我设法通过attachment.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"
Run Code Online (Sandbox Code Playgroud)

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

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