outlook = win32com.client as win32
mail = outlook.CreateItem(0)
for j in range(list)
file = "C:desktop\\image{}.png".format(j)
attachment = mail.Attachments.Add(file)
index = "ID{}".format(j)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", index)
mail.HTMLBody = "<html><body> image <img src = ""cid:ID%s""></body></html>" % (j)
mail.Send()
目的是将所有图像(“ list”定义的最大图像数(0,1,2,...最大))嵌入到电子邮件中。但是现在停留在for循环中。请问有什么方法可以达到这个目的?谢谢!
答案 0 :(得分:0)
在互联网上进行了深入研究之后,我发现对此特定问题有所顾忌。解决方案是导入base64并使用它生成一个图像标签。我将在此处发布我的代码,以便您可以自己尝试。
import win32com.client as win32
import base64
def send_email(subject, text, list_img_paths):
outlook = win32.Dispatch('outlook.application')
myItem = outlook.CreateItem(0)
myItem.Subject = subject
list_email_img = []
img_tag = ''
for i in range(len(list_img_paths)):
list_email_img.append(base64.b64encode(open(list_img_paths[i], 'rb').read()).decode('utf-8').replace('\n',''))
img_tag +='<img alt="" src="data:image/png;base64,{}"/>'.format(list_email_img[i])
myItem.HtmlBody = """\
<html>
<head>{}</head>
<body>
<p>{}<br>
</p>
</body>
</html>
""".format(text, img_tag)
myItem.To = "example@company.com"
myItem.CC = 'example@example.org'
myItem.Display()
myItem.Send()
希望这会有所帮助