I can't determine if there is an R package that would allow me to read emails from smtp or pop server. I see plenty on sending emails and that works fine. Downloading them from the server is eluding me.
Thanks.
I can't determine if there is an R package that would allow me to read emails from smtp or pop server. I see plenty on sending emails and that works fine. Downloading them from the server is eluding me.
Thanks.
IMAP is the protocol used for receiving emails whereas SMTP is the protocol for sending emails. POP3 is another protocol for receiving emails, but its use has been drastically decreasing since IMAP has way more functionalities.
So, you are looking for a package that implements IMAP capabilities for R. For this, you can use the mRpostman package, which is an IMAP client for R. You will be able to do mailbox/folders operations, message searching using single or multiple criteria, message and attachment fetching, etc - all from inside R.
For a thorough introduction to mRpostman's functionalities, please take a look at the "mRpostman Basics" vignette: https://allanvc.github.io/mRpostman/articles/basics.html
A simple example of how to use the package:
EDIT MARCH 2021
From version v0.9.X onwards, mRpostman started to adopt R6 syntax. Therefore, most of the functions from previous versions became methods. That means they now should be called in the connection_object$method() fashion. Other features, such as OAuth2 support, were added (see http://allanvc.github.io/mRpostman/).
An example for Office 365 mail accounts:
step 1) configure your connection to the Outlook server:
Note that the url "imaps://outlook.office365.com" is the default for servers that use Office 365.
con <- configure_imap(url="imaps://outlook.office365.com",
username="your_user@company.com",
password=rstudioapi::askForPassword())
# other IMAP providers that were tested: Hotmail ("imaps://imap-mail.outlook.com"),
# Gmail (imaps://imap.gmail.com), Yahoo (imaps://imap.mail.yahoo.com/),
# AOL (imaps://export.imap.aol.com/), Yandex (imaps://imap.yandex.com)
NOTE: If you don't want to use OAuth2 and want to connect to GMail, Yahoo, AOL or other free mail provider, you might need to enable the "less secure apps access" option. Please refer to: https://allanvc.github.io/mRpostman/articles/basics.html#allowing-less-secure-apps-access
step 2) In order to search or fetch messages, we need to select a mail folder first. In this example, we select "INBOX":
con$select_folder(name = "INBOX")
step 3) Single search:
Here, we mock a message search operation inside "INBOX". We search for messages containing the string "@k-state.edu" in the "FROM" field of the message header. There are many more search functions that enable a myriad of other search possibilities (refer to the package's documentation).
res1 <- con$search_string(expr = "@k-state.edu", where = "FROM")
step 4) Message fetching:
Here we fetch the TEXT part of the message ids returned in step 3. mRpostman will create a "user@server.com/INBOX" directory containing txt files with the fetched message part.
res1 %>%
con$fetch_text(write_to_drisk = TRUE)
step 5) Extracting or fetching attachments.
Alternative 1: direct fetching
res1 %>%
con$fetch_attachments()
Alternative 2: extract attachments after a text/body fetching operation:
res1 %>%
con$fetch_text() %>% # or with fetch_body
con$get_attachments()
The results will be something like this:
There are many many many more possibilities of message searching and many other IMAP operations you can do with mRpostman. The package's vignettes and th documentation are a good start.