【问题标题】:How to send email from contact form in R Shiny如何从 R Shiny 中的联系表发送电子邮件
【发布时间】:2020-05-10 16:32:57
【问题描述】:

我在 Shiny 应用程序中实现了一个联系表单,并使用包“blastula”及其函数“smtp_send”将消息发送到发件人提供的电子邮件地址。代码是:

library(shiny)
library(blastula)
library(shinyAce)

ui = fluidPage(
  fluidPage(
    wellPanel(
      textInput("from"   , "From:"   , value="...@..."),
      textInput("to"     , "To:"     , value="...@..."),
      textInput("subject", "Subject:", value="This is the subject"),

      p("Message", style="font-weight: bold;"),
      aceEditor("msg", value="This is the message"),
      br(),
      actionButton("send", "Send email!")
    )
  )   
)

server <- function(input, output)
{
  observe(
  {
    if(is.null(input$send) || input$send==0) return(NULL)

    Email = compose_email(body = input$msg, header = NULL, footer = NULL, title = NULL)
    Credentials = creds_anonymous(host = "smtp...", port = 25, use_ssl = TRUE)

    smtp_send(email = Email, to = input$to, from = input$from, subject = input$subject, credentials = Credentials)
  })
}

shinyApp(ui = ui, server = server)

在本地运行此脚本并使用 port=25 会给出错误消息:“警告:curl::curl_fetch_memory 中的错误:RCPT 失败:550”。

使用 port=465 或 port=587 会给出错误消息:“警告:curl::curl_fetch_memory 中的错误:已达到超时:[smtp...:465] 连接在 10000 毫秒后超时”。

在使用 port=25 的服务器上运行脚本时,会出现错误消息:'curl::curl_fetch_memory 中的错误:Recv failure: Connection reset by peer'。

我查看了smtp服务器的地址,应该是对的。它是一个不需要帐户的服务器。

有人知道为什么代码不起作用吗?

【问题讨论】:

    标签: r curl shiny shiny-server blastula


    【解决方案1】:

    我已经提取了我如何使用我的应用程序管理电子邮件问题的方式 - gmail 方式适用于 Outlook 一种我不确定,因为我有一段时间没有检查它。

    详情替换为自己的,gmail方式先评论。

    下面的代码,忽略中间的斯洛文尼亚语。他们还检查某人何时可以发送和发送电子邮件以及地址是否正确等等......

    这可能会有所帮助..

    编辑:必须定义问题,并且您的电子邮件地址应该是 gmail.com。

    library(shiny)
    # library (RDCOMClient)
    Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre1.8.0_241/') 
    library(mailR)
    library(shinythemes)
    library(shinyjs)
    
    ui = fluidPage(
      useShinyjs(),
      fluidPage(
        br(),
        fluidRow(
          column(12,align="center",
                 h1(icon("envelope", lib = "font-awesome"))),
          br(),
          column(12,align="center",
                 textInput("telo", "Problem statement","", width = "400px",
                           placeholder= "obvezno izpolnite!"),
                 br(),
                 textInput("kontakt","Your working email - necessary!","",
                           placeholder = 'ime.priimek@gmail.com'),
                 helpText("Test email= Thorin@gmail.si"),
                 actionButton("send", "Send",icon("fas fa-arrow-up", lib = "font-awesome")),
                 br())),
        br(),
        fluidRow(
          column(12,
                 br()))
      )
    
    
    
    )   
    
    
    server <- function(input, output,session)
    {
      disable("send")
      ###################################################################################### pošlji email, telo strani
      testek11<-function(failed = FALSE) {
        modalDialog(
          title="",
          fluidRow(column(12,align="center",
                          "SEND.")),
          br(),
          easyClose = FALSE,
          footer=fluidRow(column=12,align="center",
                          modalButton("OK",icon=icon("fas fa-check-circle"))
          )
        )
      } 
    
      observeEvent(input$send,{
        #showModal(testek21())
        shinyjs::disable("send")
        shinyjs::disable("nazaj2")
        shinyjs::disable("telo")
        shinyjs::disable("kontakt")
    
    
        ## gmail way
        Sys.sleep(2)
    
        # send.mail(from = paste(input$username,"@domain.com",sep=""),
        #           to = c("test99@gmail.com"),
        #           subject = paste("Aplikacija NNP: Problem:",input$kontakt,sep=" OD "),
        #           body =  paste(input$telo,input$username,sep=" // OD // "),
        #           smtp = list(host.name = "smtp.gmail.com", port = 465,
        #                       user.name = "test99@gmail.com",
        #                       passwd = "pass", ssl = TRUE),
        #           authenticate = TRUE,
        # send = TRUE)
    
    
        ## outlook way -- possibly outdated
    
        # OutApp <- COMCreate("Outlook.Application")
        # outMail = OutApp$CreateItem(0)
        # outMail[["To"]] = "ur email adrres to where you want to send -- you can make it reactive"
        # outMail[["subject"]] = "subject"
        # outMail[["body"]] <- "body"
        # outMail$Send()
        # 
        shinyjs::enable("send")
        shinyjs::enable("nazaj2")
        shinyjs::enable("telo")
        shinyjs::enable("kontakt")
        showModal(testek11())
        updateTextInput(session,"telo",value="",placeholder = "Obvezno izpolnite!")
        updateTextInput(session,"kontakt",value="",placeholder = 'ime.priimek@gmail.si')
      })
    
      ##### enabling maile pa to kdaj lahko klikne send
    
      mailhelp <- reactiveValues(hmm=FALSE)
      mailhelp2 <- reactiveValues(hmm2=FALSE)
    
    
    
      observeEvent(input$telo,{
        if(input$telo != ""){
          isolate(mailhelp$hmm <- TRUE)
        }
        else{
          isolate(mailhelp$hmm <- FALSE)
        }
      })
    
      observeEvent(input$kontakt,{
        if(grepl("@gmail.com",input$kontakt) |grepl("@GMAIL.CON",input$kontakt) ){
          isolate(mailhelp2$hmm2 <- TRUE)
        }
        else{
          isolate(mailhelp2$hmm2 <- FALSE)
        }
      })
    
      observe({
        if(mailhelp2$hmm2 & mailhelp$hmm){
          shinyjs::enable("send")
        }
        else{
          shinyjs::disable("send")
        }
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢!我也很感兴趣,为什么使用 blastula 包中的功能还不起作用。 blastula 的优点是不需要 Java。
    • 我对囊胚一无所知,因为我从不厌倦它。公平地说,这个应用程序也有 2 年历史了,我相信现在有更好的解决方案——但是,只要它有效,就不要 *** 使用它:D...
    猜你喜欢
    • 2020-05-20
    • 2014-09-11
    • 2012-02-25
    • 1970-01-01
    • 2018-04-15
    • 2019-10-03
    • 2020-05-05
    • 2020-12-07
    • 1970-01-01
    相关资源
    最近更新 更多