![](/img/trans.png)
[英]Detect sending (or not) of Outlook mail sent by Python (with win32com)
[英]Adding width and height to local image in new mail in outlook with win32com(Python)
为什么我不能在 img 标签中将宽度和高度传递给本地图像。 下面的代码正在工作(正在显示图像)但是当我将width="100" height="200"
到 img 标签时,图像未显示。 当我将src
更改为 web 上的图像时src="https://i.stack.imgur.com/GafDJ.jpg?s=256&g=1"
并添加 width="100" height="200" 图像显示为添加的维度。
import os
import win32com.client as client
outlook = client.Dispatch('Outlook.Application')
mail = outlook.CreateItem(0)
icon = os.getcwd() + '\\test.jpg'
html_body = f'''
<img src="{icon}">
<!-- <img src="https://i.stack.imgur.com/GafDJ.jpg?s=256&g=1" width="100" height="200"> -->
'''
mail.To = ''
mail.CC = ''
mail.Subject = ''
mail.HTMLBody = html_body
mail.Save()
本地图像需要作为附件添加,否则实际图像不会与电子邮件一起发送。 过去对我有用的过程是将图像文件添加为附件,然后将其作为邮件中 html 的属性进行引用。
例如:
import os
import win32com.client as client
outlook = client.gencache.EnsureDispatch('Outlook.Application')
mail = outlook.CreateItem(client.constants.olMailItem)
icon = os.getcwd() + '\\Test.jpg'
#Add a new attachment, putting 0 as third argument means the file is hidden
att = mail.Attachments.Add(icon,client.constants.olByValue,0)
#Create a Content Id (cid) for this image. Can be whatever you want: I'm using short filename
cidName = icon.split('\\')[-1]
att.PropertyAccessor.SetProperty('http://schemas.microsoft.com/mapi/proptag/0x3712001F', cidName);
imgTag = '<img src="cid:{0:}" width="100" height="200">'.format(cidName)
mail.To = ''
mail.CC = ''
mail.Subject = ''
mail.HTMLBody = imgTag
mail.Save()
我不是整个“模式”如何运作的专家,但它似乎可以完成这项工作。 注意。 我使用了gencache.EnsureDispatch()
而不仅仅是Dispatch()
。 我发现这可确保 Outlook 常量可用(例如olByValue
),而不仅仅是使用硬编码数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.