1

I have a R code which after doing a bunch of steps sends out an email in the end. When I run this on RStudio, the entire code runs fine and sends out the email. However, when I run this via Windows Task Scheduler, the code still runs fine (and does what is intended) but does not send out the email. I have tried both RDCOMClient and sendmailR, and the problem persists.

Here is how the task is scheduled.

Program/script: "C:\Program Files\R\R-3.1.3\bin\x64\Rscript.exe" Add arguments: datavalv3.R Start in: C:\BLP\Projects\Project_07

Here is the code part with the email (with RDCOMClient)

library(RDCOMClient)

path_name <- file.path(mainDir, subDir)
subject <- paste0("Data quality checks completed for ", analysis_date)
body <- paste0("Data summary has been compiled for all the farms for ",    analysis_date,". All the data summaries are saved in the folder <", path_name, ">.")

email_fn <- function(recipient) {

  OutApp <- COMCreate("Outlook.Application")
  outMail = OutApp$CreateItem(0)
  outMail[["To"]] = recipient
  outMail[["subject"]] = subject
  outMail[["body"]] = body
  outMail$Send()  

}

email_fn(recipient = "person1@abc.com")

Here is the code part with the email (with sendmailR)

library(sendmailR)  

from <- "person1@abc.com"
to <- c("person2@abc.com","person3@abc.com")
subject <- "Email Subject"
body <- "Email body."                     
mailControl = list(smtpServer = "tucson.websitewelcome.com")

sendmail(from = from, to = to, subject = subject, msg = body, control = mailControl)

Any idea what might be the problem?

This is an update: Figured out the problem. In the task scheduler security options, earlier I had checked "Run whether user is logged on or not". I un-checked this and checked "Run only when user is logged on" - this did the trick and the emails are going through.

Ankur
  • 35
  • 6
  • Are you sure the library `sendmailR` is properly loaded, when the script is executed as Task Scheduler? I had similar problems, but on Linux, and the case was that I had installed a package in my personal folder, and when scheduler run it, it had no path to my personal folder to find the package. So maybe print result of .libPaths() as a normal user and as a Windows scheduler and compare. – bartoszukm Jan 28 '16 at 19:37
  • Thanks @bartoszukm. The code utilizes 3 libraries (RODBC, data.table & sendmailR) and all these are there in the same path (in my user's folder). If RODBC and data.table are loading properly, can sendmailR still may not? – Ankur Jan 28 '16 at 20:04
  • Did you ever solve this issue Ankur? – BSHuniversity Oct 11 '19 at 17:44
  • Have similar issue. And in my case I need to tick the 'Run whether user is logged on or not'. Did you happen to find the root cause? – Praneeth Munuganti Nov 07 '19 at 17:02

1 Answers1

0

when you try to send emails by Task Scheduler, it's just skip that email portion due to some reason(not sure why). Here is the trick, try using taskscheduleR Package. It will also send email even when you logged out. Please install other dependent packages as well.

install.packages('data.table')
install.packages('knitr')
install.packages('miniUI')
install.packages('shiny')
install.packages("taskscheduleR", repos = "http://www.datatailor.be/rcube", type = "source")

enter image description here