'How to send emails with delayed delivery in Outlook from R?

Using the RDCOMClient package in R, I am sending a series of automated emails from R. The emails need to be sent at a specific date and time, but I cannot figure out what elements in the outMail object to manipulate to send emails with delayed delivery.

Here's a simple example of what I am doing:

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("[email protected]", 
                                 "[email protected]", 
                                 "[email protected]" ), 
                       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()

}

I've tried looking through the MailModule object list here, but I can't find anything to use - and I'm not sure how to inspect the outMail object created above (e.g., str() doesn't work on this kind of object).

Thanks.

*** Exact Solution Based on Accepted Answer ***

# 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 



Solution 1:[1]

Set the MailItem.DeferredDeliveryTime property - https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.deferreddeliverytime

You don't need the MailModule object. Take a look at the live Outlook objects with OutlookSpy (I am its author) - click the Item button on the OutlookSpy ribbon.

Solution 2:[2]

since afaik Outlook needs to be running (ie online and connected) for you to achieve delayed delivery so therefore your computer also being powered up and running - why not time your R script accordingly with eg taskscheduleR

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 GWD