使用R中的RDCOMClient包,我正在从R发送一系列自动电子邮件。这些电子邮件需要在特定的日期和时间发送,但我不知道要操作outMail
对象中的哪些元素来发送延迟送达的电子邮件。
下面是我正在做的一个简单的例子:
library(data.table)
library(RDCOMClient)
# table of emails, names and send times (stt)
test_emails = data.table( First.Name = c("Joe", "Brit", "Anton"),
email = c("Joe@fakeemail.com",
"Brit@fakeemail.com",
"Anton@fakeemail.com" ),
stt = structure(c(1602270000, 1603270000, 1602470000),
class = c("POSIXct", "POSIXt"), tzone = ""))
# access Outook
OutApp <- COMCreate("Outlook.Application")
# loop over table of names/emails
for(i in 1:test_emails[,.N]){
#standard setup:
outMail = OutApp$CreateItem(0)
outMail$GetInspector()
signature = outMail[["HTMLBody"]]
outMail[["To"]] = test_emails[i, email]
outMail[["subject"]] = "Subject line"
# example body that prints the time and date of when the email is sent
outMail[["HTMLBody"]] =
paste0("<p>Hi ", test_emails[i, First.Name], ",</p>",
"<p>As discussed please find attached the detailed test instructions (PDF) and data set (CSV file)",
"You received this email at ",
gsub('^0', '', format(econ_test[i, stt], '%I:%M %p')), ' on ',
format(econ_test[i, stt], '%A, %B %d'), "</p>",
'<p>', signature, '</p>')
# sends right now. How to delay this?
outMail$Send()
}
我试过在这里查看MailModule对象列表,但找不到任何可使用的内容,也不知道如何检查上面创建的outMail
对象(例如,str()
对这种对象不起作用(。
谢谢。
***基于接受答案的精确解决方案***
# Convert the POSIXct integers to MS's time units
gmt_diff <- 4 # EDT
defer_time <- as.numeric( as.Date( test_emails[i, stt]) ) + # convert to R's date values
(as.numeric(test_emails[i, stt]) - gmt_diff*3600) %% 86400/ 86400 - # convert the POSIXct time to fraction of a day
as.numeric(as.Date("1899-12-30")) # adjust for differences in origin
# Update the DeferredDeliveryTime MailItem
outMail[['DeferredDeliveryTime']] = defer_time
设置MailItem.DeferredDeliveryTime
属性-https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.deferreddeliverytime
您不需要MailModule对象。看看OutlookSpy的实时Outlook对象(我是它的作者(-单击OutlookSpyRibbon上的项目按钮。
由于afaik Outlook需要运行(即在线和连接(才能实现延迟交付,因此您的计算机也处于通电和运行状态-为什么不使用taskscheduleR 相应地调整R脚本的时间呢