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.