【问题标题】:Send Emails using Python Win32. Adding Image to the Body of the Email is not working使用 Python Win32 发送电子邮件。将图像添加到电子邮件正文不起作用
【发布时间】:2019-12-17 08:48:09
【问题描述】:

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

我正在使用 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\My Project\image001.png

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

所以基本上从函数来看,它将是 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

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    我设法通过 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"
    

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

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

    【讨论】:

    • 完美运行。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多