user3149230
user3149230

Reputation: 173

Sending email from shiny

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.

Upvotes: 12

Views: 21276

Answers (3)

Stéphane Laurent
Stéphane Laurent

Reputation: 84529

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="[email protected]"),
    textInput("to", "To:", value="[email protected]"),
    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)
  })

})

Upvotes: 10

agstudy
agstudy

Reputation: 121568

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 <- "[email protected]"
  to <- "[email protected]"
  subject <- info$subject
  body <- info$body                    
  mailControl=list(smtpServer="serverinfo")
  sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
}

Upvotes: 1

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

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:

Upvotes: 1

Related Questions