Download attachment from an outlook email using R

Viewed 11199

I receive an email every Sunday with an attachment (a zipped folder). The subject of the email never changes. I want to find the latest email with the specified subject line and download the attachment. I am new R user and so far I have only found a way to print the email body based on the subject (from one of the other questions asked on stackoverflow How to retrieve Outlook inbox emails using R RDCOMClient?). Ideally, I want to find the email with the specified subject received on a specified date and then download the attachment. Could some please point me in the right direction. Any help will be greatly appreciated. Thank you.

2 Answers

Having read the comments in the fantastic original solution by mdneuzerling, I wanted to add an additional answer with an example of how to extract data from a subfolder. This was something I only recently figured out and borrows a lot from the original solution.

Say your primary email was user@outlook.com and within that address you have a folder titled Inbox. Additionally, say you have a subfolder in your Inbox called Important. If you wanted to download an attachment from this subfolder from an email titled 'Email with Attachment', you would define the file path as follows:

path <- outlookNameSpace$Folders("user@outlook.com")$Folders("Inbox")$Folders("Important")$FolderPath()

You would then use the AdvancedSearch method as specified in mdneuzerling's answer through the following code:

   search <- outlook_app$AdvancedSearch(
             paste0("'", path, "'"),
             "urn:schemas:httpmail:subject = 'Email with Attachment'"
)

By following the rest of the method outlined in mdneuzerling's solution, you should be able to extract the attachment from the subfolder.

Related