使用电子邮件

时间:2017-12-29 14:12:17

标签: r sqlite shiny

我有一个名为users的表格,其中有3列(usernamepasswordemail)。当用户忘记密码时,他需要插入他的电子邮件以通过电子邮件接收密码(密码恢复)。

我有两个问题:

  1. 为什么我总是收到消息User does not exist
  2. 在第二个observe中,是否有更有效的方法来获取passmsg
  3. 首先:

    observe({
        if (USER$Foget == TRUE){           # Foget is a flag
            if (!is.null(input$back)){     #back is a button to go back to the main page    
                if (input$back > 0){            
                   output$page <- renderUI({               
                   div(class="outer",do.call(bootstrapPage,c("",ui1())))       
                   })
                } 
            }
        }
        if (USER$Foget == TRUE){
            if (!is.null(input$PassRecall)){     #PassRecall is a button to send the email
                if (input$PassRecall > 0){
                    Mail_to <- isolate(input$email)
                    query <- sprintf({"
                       SELECT password 
                        FROM users 
                        WHERE email='%s'"}, 
                        Mail_to, serialize=F) 
                        db   <- RSQLite::dbConnect(RSQLite::SQLite(), dbname="db.sqlite")
                    userr <- RSQLite::dbGetQuery(db, query) 
                    RSQLite::dbDisconnect(db)
                    if ( length(userr$rowid)>=1 ) {
                        USER$existed <- TRUE
                    }
                } 
            }
          print(ui)
        }})
    

    第二

    observe({
        if (USER$Foget == TRUE) {
          if (!is.null(input$PassRecall)){  
             if (input$PassRecall > 0){     
               if(USER$existed == TRUE){
                 Mail_from <- "estersilva1990@gmail.com"
                 Mail_to <- isolate(input$email)
                 subject <- "password recovery"
                 pass <- unique(dbGetQuery(db,"SELECT password FROM users WHERE email=='Mail_to'"))
                 msg <- "You password is: 'pass'"
                 sendmail(Mail_from, Mail_to, subject, msg)
                 showNotification("Please, Check your Email !", duration = 5, type = c("message"))                              
                }
              }
           }
        }
        if (USER$Foget == TRUE) {
            if (!is.null(input$PassRecall)){
                if (input$PassRecall > 0){
                    if(USER$existed == FALSE){
                        showNotification("User does not exist !", duration = 5, type = c("error"))              
                    }
                }
            }
        }})
    

1 个答案:

答案 0 :(得分:0)

我修好了如下:

我混合了第一次和第二次观察。另外,我将if ( length(userr$rowid)>=1 )条件更改为if(nrow(pass_retrived)>=1)

observe({
    if (USER$Foget == TRUE){
        if (!is.null(input$back)){
            if (input$back > 0){
               output$page <- renderUI({
               div(class="outer",do.call(bootstrapPage,c("",ui1())))
               })
            } 
        }
    }

    if (USER$Foget == TRUE){
        if (!is.null(input$PassRecall)){
            if (input$PassRecall > 0){
                Mail_to <- isolate(input$email)
                query <- sprintf({"
                   SELECT password 
                    FROM users 
                    WHERE email='%s'"}, 
                    Mail_to, serialize=F) 
                    db <- RSQLite::dbConnect(RSQLite::SQLite(), dbname="db.sqlite")
                pass_retrived <- RSQLite::dbGetQuery(db, query) 
                RSQLite::dbDisconnect(db)
                if(nrow(pass_retrived)>=1) {
                    Mail_from <- "YOUREMAIL"
                    subject <- "password recovery"
                    msg <- sprintf("Your password is %s.", pass_retrived)
                    send.mail(from = Mail_from, to = Mail_to, subject = subject, body = msg ,smtp = list(host.name = "smtp.gmail.com", port = 465,user.name="YOUREMAIL", passwd="PASSWORD", ssl=TRUE), authenticate = TRUE, send = TRUE)
                    showNotification("Please, Check your Email !", duration = 5, type = c("message"))
                }
                if(nrow(pass_retrived)==0) {
                    showNotification("User does not exist !", duration = 5, type = c("error"))
                }
            } 
        }
      print(ui)
    }
  })