使用RDCOMClient搜索Outlook收件箱

时间:2019-01-30 15:32:05

标签: r rdcomclient

我正在尝试使用RDCOMClient在Outlook收件箱中搜索电子邮件中的特定主题,然后获取附件。 我只用一封电子邮件就可以完成这项工作,但是由于主题包含一个date元素,因此我需要将搜索作为like子句,但无法完全理解下面的查询内容。

from glob import glob

excel_names = glob.glob('*JAN_2019-jan.xlsx')

files = []
for names in (excel_names):
   files.extend(names)

print(files)

我只需要搜索主题行的第一部分,查找日期和时间之前的所有内容。

1 个答案:

答案 0 :(得分:1)

我认为类似的事情应该起作用。它应该搜索包含指定短语的任何邮件,然后下载其每个附件。

library(RDCOMClient)
library(fs)

search.phrase <- 'test email executed at'

save.fldr <- tempdir() # Set a root folder to save attachments into
print(save.fldr)

outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  paste0("http://schemas.microsoft.com/mapi/proptag/0x0037001E ci_phrasematch '", search.phrase, "'")
)

Sys.sleep(10) # Wait some time to allow search to complete

results <- search[['Results']]

for(i in c(1:results[['Count']])){ # Loop through search results
  attachments.obj <- results[[i]][['attachments']] # Gets the attachment object

  if(attachments.obj[['Count']] > 0){ # Check if there are attachments
    attach.fldr <- file.path(save.fldr, path_sanitize(results[[i]][['Subject']])) # Set folder name for attachments based on email subject

    if(!dir.exists(attach.fldr)){
      dir.create(attach.fldr) # Create the folder for the attachments if it doesn't exist
    }


    for(a in c(1:attachments.obj[['Count']])){ # Loop through attachments
      save.path <- file.path(attach.fldr, attachments.obj[[a]][['FileName']]) # Set the save path
      print(save.path)
      attachments.obj[[a]]$SaveAsFile(save.path) # Save the attachment
    }

  }
}