This vignette aims to provide an easy source of code migration examples so old mRpostman
users can transition between the old version and the new format which implements an OO approach trough the use of R6 class and methods.
This is the summary of the main modifications in the package between version 0.3.1
and 0.9.0.0
:
All main functions, except list_attachments
and the custom-search helper functions, now are methods of the R6 class ImapConf
;
The way the connection token is passed through functions (now methods) has changed. The connection handle is created inside configure_imap()
(or ImapCon$new()
) and modified with custom requests from the methods that trigger IMAP commands. As a consequence, the password is now hidden inside the curl handle C pointer, resulting in a more secure token chain. This resulted in changes in every request function. These functions (that are methods now) do not use config_handle()
anymore, and a call to curl::set_opt()
is made in every request function so that a custom request is supplied or replaced by a new one in the original handle.
The “by” argument used in search and fetch functions was replaced by use_uid
, which is a logical now with the default value set as FALSE
. This is equivalent to the former by = MSN
default configuration.
all functions that returned invisible(0L)
now return invisible(TRUE)
Users that prefer a tidy approach, only need to apply the pipe %>%
operator between search and fetch methods (or other complementary operations), in which messages’ ids are carried from one level to another. The exposition pipe %$%
is not necessary anymore.
In the following sections, we present some code migration examples between the old and the new version of mRpostman
.
Previous code:
library(mRpostman)
# Outlook - Office 365
imapconf <- configure_imap(url="imaps://outlook.office365.com",
username="your_user@company.com",
password=rstudioapi::askForPassword())
# other mail providers that were tested: Gmail (imaps://imap.gmail.com),
# Hotmail ("imaps://imap-mail.outlook.com"), Yahoo (imaps://imap.mail.yahoo.com/),
# AOL (imaps://export.imap.aol.com/), Yandex (imaps://imap.yandex.com)
New format:
library(mRpostman)
# Outlook - Office 365
con <- configure_imap(url="imaps://outlook.office365.com",
username="your_user@company.com",
password=rstudioapi::askForPassword())
# alternative
con <- ImapCon$new(url="imaps://outlook.office365.com",
username="your_user@company.com",
password=rstudioapi::askForPassword())
Although the object that is created is different (now we have an R6 ImapCon
class), the code to create a connection object is pretty much the same, but with an alternative that uses the “initialize” method of the R6 class.
Other useful options are timeout_ms
, verbose = TRUE
, and buffersize
. Further ‘curl’ options related to IMAP functionalities can be passed to configure_imap()
, depending on the libcurl version installed on the user’s machine. See curl::curl_options()
. In this new version, there is an important curl parameter exposed, xoauth2_bearer
, which enables OAuth2.0 authentication. For more details, check the “IMAP OAuth2.0 authentication in mRpostman” vignette.
Previous code:
New format:
In the new format, the previously selected folder is kept during the session unless you execute a new selection.
All search functions now return only the message ids.
Previous code:
result <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_before(date_char = "02-May-2019",
by = "UID",
flag = "UNANSWERED")
result$msg_id
New format:
You can also NEGATE the statement to search for messages NOT BEFORE a date:
Previous code:
results <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_before(date_char = "02-May-2019",
negate = TRUE,
by = "UID",
flag = "UNANSWERED",
esearch = TRUE)
results$msg_id
New format:
This pattern is applied to all SEARCH functions.
Special attention should be paid to the search_string
and search_flag
methods due to changes in the names and order of the arguments.
Previous code:
results <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_string(section_or_field = "TEXT", string = "Dear Allan")
New format:
Previous code:
New format:
Now you can check which flags are available in a mail folder with the list_flags()
method. Also, you can pass multiple flags as the search criterion.
Previous code:
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_on(date_char = "10-May-2019", by = "UID") %$% #exposition pipe operator
fetch_msg_metadata(imapconf, msg_id = msg_id, by = "UID",
metadata = c("INTERNALDATE", "UID", "ENVELOPE"))
New format:
You can check metadata options with metadata_options()
.
Previous code:
imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_on(date_char = "23-Sep-2019") %$%
fetch_full_msg(imapconf, msg_id=msg_id) %>%
list_attachments()
New format:
Note that list_attachment()
is an independent function. It is not a method of the ImapCon
class that depends on the connection object.
The new version brings an alternative fetching method (fetch_atachments_list()
) for listing attachments without the need of previously executing a fetch_body
or fetch_text
operation:
Previous code:
imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_on(date_char = "23-Sep-2019") %$%
fetch_full_msg(imapconf, msg_id=msg_id) %>%
get_attachments()
New format:
The new version brings an alternative fetching method (fetch_atachments()
) for downloading attachments without the need of previously executing a fetch_body
or fetch_text
operation: