如何从 R 在 Outlook 中发送延迟传递的电子邮件?
How to send emails with delayed delivery in Outlook from R?
使用 R 中的 RDCOMClient 包,我正在从 R 发送一系列自动电子邮件。电子邮件需要在特定日期和时间发送,但 我无法弄清楚 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 对象列表 here,但找不到任何可用的东西 - 而且我不确定如何检查上面创建的 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://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.deferreddeliverytime
您不需要 MailModule 对象。使用 OutlookSpy(我是其作者)查看实时 Outlook 对象 - 单击 OutlookSpy 功能区上的项目按钮。
因为 afaik Outlook 需要 运行(即在线和连接)才能实现延迟交付,因此您的计算机也已通电并且 运行 - 为什么不相应地为 R 脚本计时例如 taskscheduleR
使用 R 中的 RDCOMClient 包,我正在从 R 发送一系列自动电子邮件。电子邮件需要在特定日期和时间发送,但 我无法弄清楚 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 对象列表 here,但找不到任何可用的东西 - 而且我不确定如何检查上面创建的 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://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.deferreddeliverytime
您不需要 MailModule 对象。使用 OutlookSpy(我是其作者)查看实时 Outlook 对象 - 单击 OutlookSpy 功能区上的项目按钮。
因为 afaik Outlook 需要 运行(即在线和连接)才能实现延迟交付,因此您的计算机也已通电并且 运行 - 为什么不相应地为 R 脚本计时例如 taskscheduleR