Python如何使用win32com.client和Outlook插入图像?

时间:2018-08-09 08:22:05

标签: python html email win32com

我正在尝试使用win32com.client发送html电子邮件。这是示例代码:

import win32com.client as win32  

mail = win32.Dispatch('outlook.application').CreateItem(0)
mail.To = 'sample@sample.com'
mail.Subject = 'test'
mail.HTMLBody = html_pages

mail.Send()

我现在的解决方案是将图像上传到服务器,并将URL插入html_pages。缺点是在服务器关闭时不显示图像。有没有一种方法可以使用win32com通过电子邮件发送图像?

1 个答案:

答案 0 :(得分:0)

一种方法是将图像作为附件发送:

import win32com.client as win32  

mail = win32.Dispatch('outlook.application').CreateItem(0)
mail.To = 'sample@sample.com'
mail.Subject = 'test'
mail.HTMLBody = html_pages

attachment  = '*path to your image*'
mail.Attachments.Add(attachment)
mail.Send()

或者您可以将图像放在html正文中

import win32com.client as win32  

mail = win32.Dispatch('outlook.application').CreateItem(0)
mail.To = 'sample@sample.com'
mail.Subject = 'test'
mail.HTMLBody = html_pages + <br><img src="path">

mail.Send()

或者,如果您希望图像实际附着在身体上而不是链接到路径,请在此处查看:

I am not able to add an image in email body using python , I am able to add a picture as a attachment but i want a code to add image in mailbody