【问题标题】:R Shiny sending user-uploaded file via email attachmentR Shiny 通过电子邮件附件发送用户上传的文件
【发布时间】:2018-06-19 01:29:24
【问题描述】:

我正在尝试构建一个 Shiny 应用程序,该应用程序将接受用户上传的文件以及其他一些用户输入信息(textInput 输入),然后使用 textInput 对象向我发送一封电子邮件以填写主题、正文、等等,同时使用 mailR 包获取文件并将其附加到具有特定名称的电子邮件中。我在下面发布了我正在使用的代码(更改了电子邮件地址、用户名和密码)。

代码

# load packages
library(shiny)
library(rJava)
library(mailR)
library(timeDate)

##############
##### ui -----
##############
ui = fluidPage(
        fluidRow(
             wellPanel(title = "Submit Data to the Database", 
                textInput("name", label = "Name:", placeholder = "Name"),
                textInput("email","Email Address:"),
                textInput("inst","Institution:"),
                textInput("notes","Notes:", placeholder = ""),
                fileInput("userdata", label = "Choose CSV File", 
                    accept = c(
                      "text/csv",
                      "text/comma-separated-values,text/plain",
                      ".csv")),
                actionButton("submitdata",label = "Submit Data") 
                 )
            ))

##################
##### server -----
##################
server <- function(input, output) {
  observeEvent(input$submitdata, {
    isolate({
      send.mail(from = "myemail@gmail.com",
          to = "myotheremail@gmail.com",
          subject = paste("New data submitted to WoodDAM from", input$name, sep = " "),
          body = paste(input$name, "from institution:", input$inst, "with email:", input$email, 
            "has submitted data to the wood jam dynamics database.",
            "This data is attached to this email",
            input$name, "sent the following note accompanying this data:", input$note, collapse = ","),
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "myusername", passwd = "mypassword", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE,
          attach.files = input$userdata,
          file.names = paste(timeDate(Sys.time(), FinCenter = "America/Denver"), input$name, "WooDDAM_user_data", sep = "_", collapse = ","), # optional parameter
          debug = T)
    })
  })

}
shinyApp(ui = ui, server = server)

当我在外部查看器(chrome 浏览器)中运行此应用程序时,我可以输入文本并获取要发送的电子邮件,但是当我上传测试 .csv 文件并单击提交按钮时,它会返回显示的错误以下。

错误

Listening on http://127.0.0.1:3587
Warning: Error in .createEmailAttachments: If not NULL, length of argument 'file.names' must equal length of argument 'file.paths'
Stack trace (innermost first):
    77: .createEmailAttachments
    76: send.mail
    69: isolate
    68: observeEventHandler [#3]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>

当我通过注释掉 file.names 参数并认为这会解决问题时,我收到以下错误:

第二次错误

Warning: Error in file.exists: invalid 'file' argument
Stack trace (innermost first):
    78: file.exists
    77: .createEmailAttachments
    76: send.mail
    69: isolate
    68: observeEventHandler [#3]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>

关于如何让这个应用程序发送上传的 csv 文件而不会引发错误,是否有人有任何建议?我怀疑当我尝试将 input$userdata 提供给 send.mail 的 attach.files 参数时我做错了,但我不确定如何使该输入成为电子邮件附件。虽然文件名问题很烦人,但我可以忍受无法更改文件名,只要上传的文件附加到电子邮件中。

感谢您提供的任何帮助!

【问题讨论】:

    标签: r email shiny email-attachments


    【解决方案1】:

    它不起作用的原因是您将对象input$userdata 提供给send.mail 函数而不是filepath

    如果打印出input$userdata的内容,可以看到它包含以下属性:

    • 姓名
    • 尺寸
    • 类型
    • 数据路径

    您应该像这样传递数据路径:attach.files = input$userdata$datapath

    这样语法得到纠正,但如果您热衷于使用 gmail,您可能仍需要在 google account 上启用不太安全的应用程序。

    【讨论】:

    • 效果很好,谢谢。它还解决了 file.names 与 file.paths 长度不匹配的问题。
    • 嗨@GyD,你能更具体地说明如何打印输入$userdata吗?
    猜你喜欢
    • 2017-12-22
    • 2020-04-19
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2015-11-10
    • 1970-01-01
    相关资源
    最近更新 更多