【发布时间】:2017-06-17 11:09:51
【问题描述】:
我正在使用 Python 3.5 自动化一些工作电子邮件。我们不能使用 smtp。必须通过win32com控制outlook来完成。我已经全部工作了。我可以创建一个 html 电子邮件。我可以给它一个电子邮件地址,如果它是打开的 Outlook 配置文件中的活动电子邮件地址,它将从该地址发送。我可以添加抄送、密送、主题等。
我的问题是添加存储在 Outlook 中的签名。我已经读过 Outlook 签名没有暴露,因此我不能像 SendUsingAccount 或 mail.CC 字段那样简单地调用它们。
我的替代方法是让用户使用 tkinter 和 askopenfilename() 指定文件。
这在大多数情况下都有效。用户指定签名文件位置一次。我将它存储在我的 tkinter 程序在启动时读取的数据文件中。用户生成电子邮件并发送。
问题是我正在从签名文件中读取 HTML 并将其存储在一个变量中,然后将其附加到现有的 HTMLBody。
它有效。但是签名文件 HTML 中包含的任何图片都不会显示。
我尝试编写一个额外的脚本,将 html 文件中图片的简写地址替换为 FULL 目录,希望能够修复它。不行。
有什么方法可以确保签名文件中包含的任何图片都实际显示在电子邮件中???
这是我的签名文件 HTML 阅读器/编辑器
def altersigfile(file):
fullDir = file
newDir = fullDir.rsplit("\\",1)[0]
subDir = fullDir.rsplit("\\", 1)[1]
subDir = subDir.replace(" ", "%20")
subDir = subDir.strip(".htm")
subDir += "_files"
newDir += "/" + subDir
newDir = newDir.replace("\\", "/")
newsigfile = ""
with open(fullDir,"r") as file:
for line in file:
if "src=" in line:
check = ""
newline = ""
check = r'src="' + subDir
newline = re.sub(subDir, newDir, line)
newsigfile += newline
else:
newsigfile += line
return newsigfile
这是我的电子邮件脚本:
def emailer(sendFrom, sendTo, subject, cc, bcc, body, sigfile=None, auto=False, useSig=False):
ol = win32.Dispatch('Outlook.Application')
msg = ol.CreateItem(0)
#DO NOT CHANGE: the below script is the only way you were able to get outlook to register what email you give it.
b=False
for i in range(1,ol.Session.Accounts.Count+1):
if ol.Session.Accounts.Item(i).SmtpAddress == sendFrom:
sendFrom = ol.Session.Accounts.Item(i)
b=True
break
if b==False:
messagebox.showinfo("Invalid Address",
"""The email address you entered ({email}), is not an authorized outlook email address. Please verify the email address you provided and make sure it is authorized in your outlook.""".format(email = sendFrom))
return
msg._oleobj_.Invoke(*(64209, 0, 8, 0, sendFrom))
#end of DO NOT CHANGE
msg.To = sendTo
msg.Subject = subject
if cc:
msg.CC = sendFrom
if bcc:
msg.BCC = ol.Session.Accounts.Item(1).SmtpAddress
msg.HTMLBody = body
#currently this does add the sigfile html file held by outlook. However... the picture doesnt load. :(
if useSig==True:
signature = altersigfile(sigfile)
msg.HTMLBody += signature
print(msg.HTMLBody)
所以重申一下。除了签名文件中包含的图片不会显示之外,上述所有方法都适用。这是唯一错误的事情,我似乎无法找到使其工作的方法。 任何帮助将不胜感激。谢谢。
附:抱歉第二批代码中的格式。对于我的生活,我似乎无法正确格式化。
【问题讨论】: