rpremraj / mailR

A utility to send emails from the R programming environment
http://rpremraj.github.io/mailR/
190 stars 57 forks source link

Error when trying to send e-mail automatically using launchd on Mac #18

Closed kkoshijima closed 9 years ago

kkoshijima commented 9 years ago

Hi!

I am really enjoying this package, but I've run into this one problem which I can't figure out.

Below is my code for sending the email. I am trying to send it via Exchange.

sendReportMail <- function(body){
    to <- "to@example.com"
    subject <- as.character(Sys.time())
    from <- "from@example.com"
    smtp <- list(
        host.name = "smtp.office365.com",
        port = "587",
        domain = "domain.com",
        user.name = 'XX',
        passwd = 'YY',
        authentication = 'login',
        tls = TRUE  
        )
    send.mail(from = from, to = to, subject = subject, body = body, smtp = smtp, authenticate = TRUE, encoding = "utf-8", inline = TRUE, debug = TRUE)
    return("E-mail sent!")
}

sendReportMail("Message!")

The code works well when I execute it manually, but it gives the error below when I execute it automatically using launchd. (It does not give out the detailed error message even though I set debug = TRUE)

Error in ls(envir = envir, all.names = private) : 
  invalid 'envir' argument
Calls: sendReportMail ... <Anonymous> -> .jrcall -> .jcall -> .jcheck -> .Call
Execution halted

Here is what I did.

i. I wrote the above code in a R script file (ex. myMail.R). ii. I made a bash script that runs the above script (ex. test.sh).

/usr/bin/Rscript  myMail.R

iii. I wrote the following launchd script.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>/Users/UserName/Documents/test.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>10</integer>
        <key>Minute</key>
        <integer>00</integer>
    </dict>
    <key>StandardOutPath</key>
    <string>/Users/UserName/Documents/test.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/UserName/Documents/error.log</string>
</dict>
</plist>

iv. I loaded the launchd script in ~/Library/LaunchAgents/

With this, I was hoping that it would send the email every morning at 10 AM, but it doesn't work. The odd thing is that it does work when I do sh test.sh manually.

Could you help me troubleshoot?

Thank you!

kkoshijima commented 9 years ago

There seems to be other people who are struggling with the same problem.

http://apple.stackexchange.com/questions/92406/mail-does-not-work-in-scripts-launched-with-launchd

I've set AbandonProcessGroup to be true, but it's still not working... Any ideas?

rpremraj commented 9 years ago

Sorry, it appears that this is not a mailR issue.

kkoshijima commented 9 years ago

Yes, it does seem so.
But still I'm assuming that sending emails automatically is a common way of how people would want to use mailR, and I think there are many other people who are relevant to this question.

Any suggestions on how you would use mailR in an automated fashion other than using launchd?

dj68 commented 9 years ago

I got a very similar problem using mailR with R3.1.3 on a windows. Running a script like the one above interactive works just fine!

But when I am trying to run it non interaktiv e.g. in cmd:

"C:\Program Files\R\R-3.1.3\bin\i386\R.exe" --no-save <SimpleTestMail.R >SimpleTestMail.out

it does not work with the same error message:

error in ls(envir = envir, all.names = private) : 
   unvalid 'envir' argument`
Calls: <Anonymous> -> .jrcall -> .jcall -> .jcheck -> .Call

I set up the email first with:

email <- send.mail(..., send = FALSE)

then the command

email$send()

actually throws the error

dj68 commented 9 years ago

I found out, what the problem was (for me): The McAfee antivirus software was blocking outgoing traffic on all ports for (allmost) all programs. Rgui.exe was on the exception list, whereas Rterm.exe wasn't. Adding an exception for Rterm.exe (outgoing) on port :25 fixed the problem

rpremraj commented 9 years ago

Oh dear! Glad you figured it out. Have fun..