3

I have a daily email that is sent to me and within that email, there is a download link. When you click on the download link, there is a csv file that gets downloaded with a different file name every time. On top of this, the download link name changes daily.

Is there a way to call text in the body of an email that is a hyperlink, and read that downloaded file to R?

My email looks like this:

enter image description here

My usual code to read anything from an email looks like this:

##Load Libraries
library(readr)
library(RDCOMClient)
library(plotrix)
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  "urn:schemas:httpmail:subject = 'SUBJECT NAME'"
)

Sys.sleep(5) # Wait a hot sec!


results <- search$Results() # Saves search results into results object

Sys.sleep(5) # Wait a hot sec!

results$Item(1)$ReceivedTime() # Received time of first search result

as.Date("1899-12-30") + floor(results$Item(1)$ReceivedTime()) # Received date

# Iterates through results object to pull out all of the items
for (i in 1:results$Count()) {
  if (as.Date("1899-12-30") + floor(results$Item(i)$ReceivedTime()) 
      == as.Date(Sys.Date())) {
    email <- results$Item(i)
  }
}

attachment_file <- tempfile()
email$Attachments(1)$SaveAsFile(attachment_file)

##Automatically Determine csv file name
file_name<-unzip(attachment_file,list=TRUE)
csv_file<-file_name$Name

##Read CSV File
df <- read_csv(unz(attachment_file,csv_file), skip = 25)
nak5120
  • 3,410
  • 3
  • 23
  • 62

0 Answers0