我正在尝试构建一个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参数时,我怀疑我做错了什么,但我不确定如何将该输入作为电子邮件附件。虽然文件名问题令人讨厌,但只要上传的文件附加到电子邮件中,我就无法更改文件名。
感谢您提供的任何帮助!
答案 0 :(得分:0)
它之所以不起作用的原因是您将input$userdata
对象提供给send.mail
函数而不是filepath
。
如果您打印出input$userdata
的内容,则可以看到它包含以下属性:
您应该像这样传递数据路径:attach.files = input$userdata$datapath
。
通过这种方式,语法已得到纠正,但如果您热衷于使用Gmail,则可能仍需要在google account上启用安全性较低的应用。