我正在使用Win32com.client模块在新的Outlook窗口中将邮件发送给带有附件的收件人,但是如果我使用计划程序发送邮件,则无法每天更新带有当前日期的邮件。我需要主题行包含发送报告的日期和时间
import win32com.client
from win32com.client import Dispatch
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Test report for"
newMail.HTMLBody = "This is for testing"
attachment = "C:\\Users\\ibm\\test.xls"
newMail.Attachments.Add(attachment)
newMail.To = "ibm@gmail.com"
newMail.Send()
print('Report mail sent Successfully!!!')
实际结果: 的测试报告
预期结果: 2019年6月27日的测试报告
答案 0 :(得分:0)
替换此
newMail.Subject = "Test report for"
使用
newMail.Subject = "Test report for" + str(today.strftime("%d/%m/%y"))
先执行此操作
import datetime
today = datetime.date.today()
答案 1 :(得分:0)
from datetime import date
import win32com.client
from win32com.client import Dispatch
today = date.today()
d1 = today.strftime("%d/%m/%Y")
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Test report for" + d1
newMail.HTMLBody = "This is for testing"
attachment = "C:\\Users\\ibm\\test.xls"
newMail.Attachments.Add(attachment)
newMail.To = "ibm@gmail.com"
newMail.Send()
print('Report mail sent Successfully!!!')
首先从datetime模块导入日期,然后使用strftime()函数获取所需格式的日期。