12

I am a new Shiny user and I am interested in creating a web app where visitors can fill in some questions (depending on random R data) and they can submit them.

My problem is to find the way to send to me that information via email, for example, each time they submit the data.

I am an university lecturer and I think this is a good way to assess my students.

micstr
  • 5,080
  • 8
  • 48
  • 76
user3149230
  • 173
  • 1
  • 1
  • 7
  • 1
    Where are you hosting your shiny server? The configuration for email will strongly depend on that, and might not even be possible if your hosting provider doesn't support it. If you are trying to do this from a local run of shiny, then the configuration will depend on the users email provision, which will be even harder to figure out. – Spacedman Dec 31 '13 at 13:22
  • At the moment I am triying to host my this web in the shiny server, but I have read that it does only work in Linux, so I have this web on my pc with runapp. – user3149230 Dec 31 '13 at 18:52

3 Answers3

9

Here is the Shiny email sender I wrote to test the sendmailR package in a Shiny app. On a Linux platform, I have not configured anything and the app perfectly works. The user types the body of the message in a text area generated and handled by the shinyAce package.

ui.R

shinyUI(pageWithSidebar(

  headerPanel("Email sender"),

  sidebarPanel(
    textInput("from", "From:", value="from@gmail.com"),
    textInput("to", "To:", value="to@gmail.com"),
    textInput("subject", "Subject:", value=""),
    actionButton("send", "Send mail")
  ),

  mainPanel(    
    aceEditor("message", value="write message here")
  )

))

server.R

library(shinyAce)
library(sendmailR)

shinyServer(function(input, output, session) {

  observe({
    if(is.null(input$send) || input$send==0) return(NULL)
    from <- isolate(input$from)
    to <- isolate(input$to)
    subject <- isolate(input$subject)
    msg <- isolate(input$message)
    sendmail(from, to, subject, msg)
  })

})
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 1
    From Windows, I get an error using that code: Unhandled error in observer: cannot open the connection – aloplop85 Jul 31 '15 at 16:58
  • This does not work on https://henkie.shinyapps.io/lalala unfortunately. – Jim Nov 27 '16 at 15:10
  • 4
    @Jim Maybe the RStudio guys have not configured the mail server on shinyapps in order that all their users do not use their server to send some emails (this is not my fault, not a reason to downvote my answer...) – Stéphane Laurent Nov 28 '16 at 18:30
  • isn´t it better to observe the input via observeEvent()? Because if an input changes after the mail was sent, the reactive Variable input$send stays at true, and everytime an other input (like $from or $two) changes the mail will be sent. – Manuel Mar 04 '19 at 14:47
  • @Manuel `input$from` and `input$to` are isolated => the observer is not reactive to them. Indeed you can use `observeEvent(input$send, {......`, and in this way you don't need to `isolate`. But `observeEvent` didn't exist at the time I wrote this answer. – Stéphane Laurent Mar 04 '19 at 14:52
  • @StéphaneLaurent thats correct. Didnt see the isolate first ... My mistake ... Maybe the observeEvent would be nice as an addition to your answer. – Manuel Mar 04 '19 at 14:55
  • Looking at this now . . . does anyone know if this works with rstudio connect? – Ben G May 20 '20 at 20:04
1

R can definitely send an e-mail. Googling for R send email leads me to the sendmailR package, which is available from CRAN. Also take a look at:

Community
  • 1
  • 1
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
1

This should be a good start :

library(shiny)
ui <- pageWithSidebar(
  headerPanel("fill this and send"),
  sidebarPanel(

  ),
  mainPanel(
    textInput("name", "Name:", ""),
    textInput("body", "Body:", ""),
    actionButton("goButton",label = "Send this")

  )
)


server <- function(input, output) {
  observe({
    # Take a dependency on input$goButton
    if (input$goButton == 0)
      return(NULL)
    # Use isolate() to avoid dependency on input$goButton
    isolate({
      info <- data.frame(subject=paste("New info from:",input$name),
                         body = info$body)
      InfromMe(info)
    })
  })
}
runApp(list(ui=ui,server=server))

where inforMe , is the mail function using PaulHimstra answer:

#####send plain email
InfromMe <- function(info){
  from <- "you@account.com"
  to <- "recipient@account.com"
  subject <- info$subject
  body <- info$body                    
  mailControl=list(smtpServer="serverinfo")
  sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
}
agstudy
  • 119,832
  • 17
  • 199
  • 261