My team receives multiple requests a day and these requests always have an attachment. The attachment is a form that someone on my team fills out a portion of and then sends back to the requestor. Part of our performance metrics is to understand how quickly we are responding to these emails.
I have an outlook folder that houses the request (Original email) and the response to the request. I've been working with the Microsoft365R package, but I'm struggling to figure out to how to extract the information I need in bulk.
Here's the code I've tried so far
library(Microsoft365R)
# Authenticate with Microsoft 365 (You may need to follow the interactive login process)
get_business_onedrive() # This will prompt you to authenticate
outlook <- get_business_outlook()
# Specify the folder name (replace 'YourFolderName' with the actual folder name)
folder_name <- "PRA Checklists"
folder <- outlook$get_folder(folder_name)
# Get emails from the folder
emails <- folder$list_emails(n = 100000)
# Check the structure of the emails object
str(emails)
ls(emails[[1]])
lapply(emails, ls)
The object emails is a list of environments.. and each environment has a list called properties which has the data I'm looking for, I just don't know how to get it all into a dataframe for each email. The data elements I'm looking to get from the properties list is c("receivedDateTime", "sentDateTime", "hasAttachments", "subject", "bodyPreview", "conversationId").
I've been able to connect to my outlook and get a list of environment objects. I've been unable to pull the data in any kind of efficient way.. I tried a for loop but failed miserably
for (i in 1:length(emails)) {
for (j in 1:length(emails[[i]]$properties)){
list[[i]] <- emails[[i]]$properties[[j]]
}
}
This one had me stumped, too.
I noticed that if you pull out your emails (I'm using my inbox rather than a specific folder):
library(Microsoft365R)
outl <- get_business_outlook()
emails <- outl$get_inbox()$list_emails(n = 20)
You can then query the first email's properties... in RStudio, after the last dollar sign, you can press "tab" and the children of "properties" will be displayed - e.g., id, recievedDateTime, subject, etc.
emails[[1]]$properties$
But you wanted to get the properties for all your emails. If we use the tidyverse, we can pull out those properties for each list item like this:
library(tidyverse)
email_df <- tibble(
id = map_chr(emails , ~.$properties$id),
receivedDateTime = map_chr(emails , ~.$properties$receivedDateTime),
emailAddress = map_chr(emails , ~ .x$properties$sender$emailAddress$address),
subject = map_chr(emails , ~ .x$properties$subject)
)
Now I have a list of my emails that I can filter and work with however I choose.