Ankur
Ankur

Reputation: 35

R code not sending email via task scheduler but otherwise runs fine

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

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

library(sendmailR)  

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

Upvotes: 1

Views: 1436

Answers (2)

Gerardo Mora M.
Gerardo Mora M.

Reputation: 3

I had the same issue and I solved by closing the outlook app using task manager, then running outlook as administrator.

Upvotes: 0

Sumit Singh
Sumit Singh

Reputation: 11

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

Upvotes: 0

Related Questions