This uses the Microsoft365R package to download email attachments via Microsoft 365. Connection will be made using connect_outlook().

download_mail_attachment(
  path = getwd(),
  filename = "mail_{name}_{datetime}",
  search = "hasattachment:yes",
  search_subject = NULL,
  search_from = NULL,
  search_when = NULL,
  search_attachment = NULL,
  folder = get_inbox_name(account = account),
  n = 5,
  sort = "received desc",
  overwrite = TRUE,
  skip_inline = TRUE,
  account = connect_outlook(),
  interactive_mode = interactive()
)

Arguments

path

location to save attachment(s) to

filename

new filename for the attachments, use NULL to not alter the filename. For multiple files, the files will be appended with a number. The following texts can be used for replacement (invalid filename characters will be replaced with an underscore):

  • "{date}", the received date of the email in format "yyyymmdd"

  • "{time}", the received time of the email in format "HHMMSS"

  • "{datetime}" and "{date_time}", the received date and time of the email in format "yyyymmdd_HHMMSS"

  • "{name}", the name of the sender of the email

  • "{address}", the email address of the sender of the email

  • "{original}", the original filename of the attachment

an ODATA filter, ignores sort and defaults to search only mails with attachments

search_subject

a character, equal to search = "subject:(search_subject)", case-insensitive

search_from

a character, equal to search = "from:(search_from)", case-insensitive

search_when

a Date vector of size 1 or 2, equal to search = "received:date1..date2", see Examples

search_attachment

a character to use a regular expression for attachment file names

folder

email folder name to search in, defaults to Inbox of the current user by calling get_inbox_name()

n

maximum number of emails to search

sort

initial sorting

overwrite

a logical to indicate whether existing local files should be overwritten

skip_inline

a logical to indicate whether inline attachments such as meta images must be skipped

account

a Microsoft 365 account to use for searching the mails. This has to be an object as returned by connect_outlook() or Microsoft365R::get_business_outlook().

interactive_mode

a logical to indicate interactive mode. In non-interactive mode, all attachments within the search criteria will be downloaded.

Details

search_* arguments will be matched as 'AND'.

search_from can contain any sender name or email address. If search_when has a length over 2, the first and last value will be taken.

Refer to this Microsoft Support page for all filtering options using the search argument.

See also

Examples

if (FALSE) { # \dontrun{
download_mail_attachment(search_when = "2021-10-28")

if (require("certetoolbox")) {
  download_mail_attachment(search_from = "groningen",
                           search_when = last_week(),
                           filename = "groningen_{date_time}")

  download_mail_attachment(search_from = "drenthe",
                           search_when = yesterday(),
                           n = 1)
}
} # }