发送主题为当前日期的邮件

Send mail with current date as subject

我正在使用 Win32com.client 模块在新的 outlook window 中向收件人发送带有附件的邮件,但是如果我使用调度程序。我需要主题行包含发送报告的日期和时间

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/06/27测试报告

替换这个,

newMail.Subject = "Test report for"

有,

newMail.Subject = "Test report for" + str(today.strftime("%d/%m/%y"))

先做这个,

import datetime
today = datetime.date.today()
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() 函数获取所需格式的日期。