使用 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