首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R Shiny中从联系人表单发送电子邮件

如何在R Shiny中从联系人表单发送电子邮件
EN

Stack Overflow用户
提问于 2020-01-24 18:45:35
回答 1查看 998关注 0票数 0

我在一个闪亮的应用程序中实现了一个联系人表单,并使用包'blastula‘和它的函数'smtp_send’将消息发送到发件人给出的电子邮件地址。代码是:

代码语言:javascript
复制
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会给出错误消息:“Warning: error in curl::curl_fetch_memory: RCPT failed: 550”。

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

当在使用port=25的服务器上运行脚本时,会给出错误消息:“curl::curl_fetch_memory中的错误:接收失败:对等重置连接”。

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

有人知道为什么代码不能工作吗?

EN

回答 1

Stack Overflow用户

发布于 2020-01-24 19:15:23

我已经提炼出了如何处理我的应用程序中的电子邮件问题的方法-- gmail的方法适用于outlook,我不确定,因为我已经有一段时间没有检查过它了。

用你自己的方式替换细节,gmail方式是第一个评论的。

下面的代码,忽略中间的slovene。他们也是一些检查,当有人可以发送和电子邮件,如果地址是正确的,等等…

可能会有帮助..。

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59894896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档