1

我想通过具有固定主题的特定电子邮件定期搜索我的 gmail 收件箱。有人知道如何自动执行此搜索吗?我正在使用 gmailr 包。任何想法将不胜感激。

最好的问候, 海伦

4

1 回答 1

1

我找到了使用 AWS 的解决方案。我在这里详细写过。

简而言之,您需要做的是:

  1. 设置 AWS 实例。
  2. 编写一个检查电子邮件的函数。我的功能如下。
  3. 设置 CronR 作业以定期运行它。

    checkEmail <- function(searchString="from: X@example.com [Test]: New Order",minutesThreshold=20){
    
      searchResults<- messages(searchString) %>% 
        unlist(.,recursive = FALSE)
    
      cat(
        paste0(
          searchResults$resultSizeEstimate," results found for ",searchString,"\n"))
    
      # extract bookings
      searchResultsDf <- dplyr::bind_rows(searchResults$messages)
    
      selectid <- vector()
    
      for(i in 1:nrow(searchResultsDf)){
        # get individual ID  
        id <- searchResultsDf$id[i]
        #extract message
        resultMessage <- message(id, format = "full")
        # Get the time received of the message
        timeReceived <- resultMessage$internalDate %>%
          as.numeric() %>% 
          `/`(1000) %>%
          as.POSIXct(origin= "1970-01-01")
    
        cat(paste0("Message received at ",timeReceived,"\n"))
    
        # if it was a long time ago ignore
        if(
          (timeReceived) < (Sys.time()-minutesThreshold*60)
        ){
          cat("No recent messages found \n")
          break
        }else{
          selectid[i] <- id
        }
      }
      return(selectid)
    }
    
于 2018-11-14T09:00:57.530 回答