Download Attachment from an Outlook Email Using R

Downloading attachment from Outlook into R

while the beginning part of the subject of the email report stays the
same, the end keeps changing.

You could use like in combination with % then:

library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
search <- OutApp$AdvancedSearch("Inbox", "urn:schemas:httpmail:subject like 'Finding Memo%'")
while (search$Results()$Count() == 0) TRUE
for (x in seq_len(search$Results()$Count())) {
print(search$Results()$Item(x)$Subject())
}

This was working last week (...), and now this is not working. (...)
If someone could help me understand what is causing first method to
break

Since it's not reproducible and you gave no info on what changed, this will be difficult.

Reading Email Attachment to R

Try putting Sys.sleep(5) (if that doesn't work, try Sys.sleep(10))in between saving as results, and results$Item(1)$ReceivedTime(). I think it needs time to process in between.

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)
}
}

Download and save Outlook message attachment with R

This does the trick:

# devtools::install_github("hrbrmstr/msgxtractr")
library(msgxtractr)
fn <- "~/mydir/mymsg.msg"
msg <- read_msg(fn)
invisible(lapply(msg$attachments, function(a)
writeBin(a$content, file.path(dirname(fn), a$filename))
))

It puts the message content in a list (see str(msg)), and saves the attachments under their original filenames.

PS: As you are refering to attachments, I assume there is no need to download anything. (It's in the nature of an attachment that it comes with the message, in contrast to example just a link to a PDF.)

How to retrieve the body of an Outlook email in my inbox using R?

Here is one approach that can be considered :

library(RDCOMClient)

## create outlook object
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace <- OutApp$GetNameSpace("MAPI")
fld <- outlookNameSpace$GetDefaultFolder(6)

# Check that we got the right folder
Cnt <- fld$Items()$Count()
emails <- fld$items
list_Text_Body <- list()

for(i in 1 : Cnt)
{
print(i)
list_Text_Body[[i]] <- emails(i)[["Body"]]
}

Sending email attachment in R via Outlook

Your issue lies in the fact that you have not escape the escape when adding the attachment

You have the below

 outMail[["Attachments"]]$Add("A:/Automate_Emails/Test_Attachment.pdf")

You it should look like this

 outMail[["Attachments"]]$Add("A:\\Automate_Emails\\Test_Attachment.pdf")

Save attachments extracted from e-mail by iterating

I've managed to do the below however, it results to an error if you receive a message WITHOUT the specified file attachment you are looking for.

library(RDCOMClient)
library(openxlsx)

setwd("C:/Users/JGGliban/Desktop/Work/ADMIN/Data Integrity/DI OT Tracker")
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
"Inbox",
"urn:schemas:httpmail:subject like '%Daily Efficiency and OT Tracker%'"
)

Sys.sleep(10)
results <- search$Results()
attachment_file <- tempfile()

date <- function(){
if ((wday(format(Sys.Date(), "%Y-%m-%d"), label = FALSE)) == 1){
return(format(Sys.Date()-3, "%Y-%m-%d"))
} else {
return(format(Sys.Date()-1, "%Y-%m-%d"))
}
}

for (i in 1:results$Count()) {
if (as.Date("1899-12-30") + floor(results$Item(i)$ReceivedTime()) == as.Date(date())) {
email <- results$Item(i)
attachment <- email$Attachments()

for(j in 1:attachment$Count()) {
if (grepl(".xlsx", attachment$Item(j)$FileName(), ignore.case = TRUE)) {
attachmentname <- attachment$Item(j)$FileName()
attachment_file <- paste0(getwd(), "/", attachmentname)
attachment$Item(j)$SaveAsFile(attachment_file)
}
Sys.sleep(10)
}
}
}


Related Topics



Leave a reply



Submit