shiny send email with attachment based on email address input

hi folks,

I have many word documents using email address as the file name, for instance,

username@mail.com.docx

I would like to make a shiny app so that the customers could input their own email address, and get an email send by shiny app with an attached docx file using email address as the file name. Is it possible to do it? Here is what I have so far, but I could not get any feedback from the app, no errors. Similar posts are

but I dont want an email to all addresses, I want an email for each of the address as they have their own attached file. Any suggestion or experience?


library(shiny)
library(mailR)

ui=fluidPage(
  titlePanel('feedbacks'),
  sidebarPanel(
    textInput('email',label='email address'),width = 3,
    actionButton('go',label='send me email')
    )
)
  

server <- function(input, output, session) {
   
  observeEvent(input$go,{
     withProgress(message = "please wait", {
       Sys.sleep(1) 
       setProgress(0.25, detail = "1 Sec")
       Sys.sleep(1) 
       setProgress(0.5, detail = "2 Sec")
       Sys.sleep(1) 
       setProgress(0.75, detail = "3 Sec")
       Sys.sleep(1) 
       setProgress(1, detail = "4 Sec")
     
       isolate({
         fullnames=list.files(path = "/Users/Desktop/test", pattern = ".docx"); 
         
         reactive({
           input$email=gsub(pattern = '\\.docx','',fullnames); 
           send.mail(from='username@foxmail.com',
                     to=input$email,
                     subject='feedbacks',
                     body=c(' '), 
                     smtp=list(host.name='smtp.qq.com',
                               port=465,                          
                               user.name='username',    
                               passwd='password',            
                               ssl=T),  
                     authenticate = T,
                     send = T,
                     attach.files = input$datapath
           )
         })
       })
       
       })
     showNotification("message sent", type = "message", duration = 10)
     

  })
}

shinyApp(ui,server)

hi @xinxi813, when I try to run your code I am getting:
Warning: Error in : Can't modify read-only reactive value 'email'
You are trying to assign a value to the input variable "email" which is not allowed, you should use another name which isn't an input.

If I understand correct your would like to filter the files by the email address the user has given. You could use grepl for that
fullnames[grepl(pattern = input$email, fullnames)]
Hope this helps

hi @ginberg Thank you very much for the quick response. I have replaced the 'input$email=gsub(pattern = '\.docx','',fullnames)' with your code, and updated some other parts of the code, I still do not get anything back. My purpose is to input the email address in shiny and get email with an attachment using the same email address as the file name.

I recommend you first solve your issue in a plain r script (not involving shiny), then after you know what code you need, its so much easier to create in shiny.

1 Like

hi @nirgrahamuk I could do the mailR part in R console successfully, I just cant figure out the reactive part of the shiny app, as the email changes.

can you make a plain script where a variable at the top containing a character string of a given email will result in the behaviour you want ?

mysel <- "someones@email.com"
... rest of script to get attachment and do a mail out ...

if you succeed at that. then mysel could be set to equal input$email once you move into shiny.

hi @nirgrahamuk do you mean like this:

library(mailR)
mysel='receiver@foxmail.com'
send.mail(from='sender@foxmail.com',
          to=mysel,
          subject='feedbacks',
          body=c('test test'),
          smtp=list(host.name='smtp.qq.com',port=465,user.name='sender',passwd='password',ssl=T), 
          authenticate = T,
          send = T,
          attach.files = c(paste0('/Users/test/',mysel,'.docx'))
          )

I made three test docx files, this code worked well on them.

If I understand correctly, 'mysel' is a variable containing a set of emails, right? Does it work in the same way as the part in my original code below?

input$email=gsub(pattern = '\\.docx','',fullnames) # this code obtain all email adresses.

After further checking out the materials online, I tried the following simple code, and it worked well. So I am guessing the problem originated from the input$email part. What is the correct way of specifying the dynamically changing email address?

ui <- fluidPage(
  actionButton("sendMail","Send Mail")
)

server <- function(input, output) {
  observeEvent(input$sendMail,{
               
                 send.mail(from = 'sender@foxmail.com',
                           to = c('receiver@foxmail.com'),
                           subject=' feedbacks',
                     body=c(' '), 
                     smtp=list(host.name='smtp.qq.com',
                               port=465,                          
                               user.name='sender',   
                               passwd='pass',         
                               ssl=T),  
                     authenticate = T,
                     send = T)
                         
               }
  )
}


shinyApp(ui = ui, server = server)

library(shiny)


ui <- fluidPage(
textInput("email","type an email"),  
  actionButton("sendMail","Send Mail"),
verbatimTextOutput("result")
)

server <- function(input, output) {

  output$result <- renderPrint({
    input$sendMail
    shiny::isolate(cat("could send an email to ", req(input$email)))
  })
  
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.