Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mailR: Sending mail to multiple addresses from shiny

Tags:

email

r

shiny

I am trying to send a mail from my Shiny app. I have created text input boxes for to address, subject and body of the mail. The app sends mail when I my input in textInput box is a single email Id. When I add the second email id, it fails. However, the same code when I try to run it from RStudio instead of a shiny app, I am able to send the emails. How do I make my textInput parse the email id correctly so that mailR sends mail to them?

My current code:

library(shiny)
library(mailR)

ui =fluidPage(
        fluidRow(
        div(id = "login",
             wellPanel(title = "Mail your report", 
                textInput("to", label = "To:", placeholder = "To:"),
                textInput("sub","Subject:"),
                textInput("msg","Message:"),
                actionButton("mailButton",label = "Send mail") 
                 )
            ))
)
server = function(input, output, session) {
 observeEvent(input$mailButton,{
                isolate({
                  send.mail(from = "[email protected]",
                  to = input$to,
                  subject = input$sub,
                  body = input$msg,
                  smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "[email protected]", passwd = "mypasword", ssl = TRUE),
                  authenticate = TRUE,
                  html = TRUE,
                  send = TRUE)
                })
            })

        }

runApp(list(ui = ui, server = server))

I am getting the following error:

Listening on http://127.0.0.1:3499
javax.mail.internet.AddressException: Illegal address in string ``[email protected], [email protected]''
    at javax.mail.internet.InternetAddress.<init>(InternetAddress.java:114)
NULL
Warning: Error in : AddressException (Java): Illegal address
Stack trace (innermost first):
    83: <Anonymous>
    82: signalCondition
    81: doWithOneRestart
    80: withOneRestart
    79: withRestarts
    78: message
    77: value[[3L]]
    76: tryCatchOne
    75: tryCatchList
    74: tryCatch
    73: .jTryCatch
    72: .valid.email
    71: send.mail
    64: isolate
    63: observeEventHandler [#3]
     1: runApp
ERROR: [on_request_read] connection reset by peer
like image 367
Apricot Avatar asked Jul 08 '16 08:07

Apricot


1 Answers

Try this, assuming the separator between emails is the semicolon:

to = unlist(strsplit(input$to, ";", fixed = TRUE))
like image 87
BogdanC Avatar answered Nov 13 '22 07:11

BogdanC