mRpostman Basics

Introduction

The goal of mRpostman is to make it easy to connect to your IMAP (Internet Message Access Protocol) server and execute commands, such as list mailboxes, search for and fetch messages in a tidy way.

In this vignette we will present all available functions of this package, but not all the possiilities one can explore.

First things first - Allowing Less Secure Apps Access

Before using mRpostman, it is essential to configure your mail account. Many mail providers today require authorizing “less secure apps” to access your account from a third part app.

See how to do it for Gmail, Yahoo Mail and AOL Mail.

Gmail

  1. Go to Gmail website and log in with your credentials.

  2. Then, go to https://myaccount.google.com/u/1/lesssecureapps?pageId=none

  1. Set “Allow less secure apps” to ON.

Yahoo Mail

  1. Go to Yahoo Mail website and log in with your credentials.

  2. Click on “Account Info”.

  1. Click on “Account Security” on the left menu.

  1. After, set “Allow apps that use less secure sign in” ON

AOL Mail

  1. Go to AOL Mail website and log in with your credentials.

  2. Click on “Options” and then on “Account Info”.

  1. Click on “Account Security” on the left menu.

  1. After, set “Allow apps that use less secure sign in” ON

Package Structure

The package is divided in 7 groups of functions:

How do I start?

1) Configuration

After enabling (if needed) “Non secure apps access” in your Mail provider, you have to configure your IMAP settings:

library(mRpostman)

# IMAP settings
# Gmail
imapconf <- configure_imap(url="imaps://imap.gmail.com",
                          username="your_user",
                          password=rstudioapi::askForPassword(),
                          verbose = TRUE
                          )

# Yahoo Mail
# imapconf <- configure_imap(url="imaps://export.imap.aol.com/",
#                           username="your_user",
#                           password=rstudioapi::askForPassword()
#                           )

# AOL Mail
# imapconf <- configure_imap(url="imaps://export.imap.aol.com/",
#                           username="your_user",
#                           password=rstudioapi::askForPassword()
#                           )

# you can try another IMAP server

2) Mailbox Commands

2.1) Listing Mailboxes

From now on, you will have to select a mailbox to issue further commands.

2.2) Selecting a Mailbox

2.3) Examining a Mailbox

Select a mailbox and count the number of existent and recent messages.

2.4) Renaming a Mailbox

3) Options Listing

Before executing search and fetch commands, it is recommended to check which are your options regarding to flags, message’s section and fields, and you server capabilities as well.

3.1) Server capabilities

Knowing you server capabilities is important when searching because you can execute an optimized search using “ESEARCH” extension. Also, if your server has the “WITHIN” extension, you can use search_younger_than() and search_older_than() functions.

If your server provides “MOVE” capabilities, you can use move_msg() for moving messages between two mailboxes.

Note that Gmail does not provide the “WITHIN” extension.

3.2) Flag Options

Flags work like tags attached to messages. You can check the options with:

3.2) Section and Fields Options

To see which section and/or fields of a message you can specify in some searches or when fetching a message, use:

3.3) Metadata Options

You can search for a specific content in a message’s metadata as well. Check your options:

5) Fetch

You can fetch full messages, message headers, message texts, or only metadata field(s) of messages.

We usually fetch messages after a search. Given the output of search functions in mRpostman, you will have to use the exposition pipe %$% in order to retrieve and correctly map the imapconf object and the msg_id (search results).

You can also choose to write fetch results to disk (working directory) using write_to_file = TRUE. If you choose to do so, mRpostman creates a folder with the mailbox name in your wd and save the parts of the messages you are fetching to text files using each message sequence number (MSN) or unique identifier (UID) as names.

5.1) Fetch Full Message

Do not forget to re-specify by = "UID" inside fetch_full_msg().

5.2) Fetch Message Header

5.3) Fetch Message Text

5.4) Fetch Message Metadata

You can check metadata options with metadata_options().

6) Miscellanea

Here we present other functions to perform useful IMAP operations.

6.1) Copy Message(s)

Copying seach results form “CRAN messages2” to “INBOX” mailbox:

6.2) Get minimum (smaller) message ID with specific flag

6.3) Get maximum (larger) message ID with specific flag

6.4) Delete message(s)

Deleting a specific “msg_id” without a previous search:

6.5) Expunge

Expunges message(s) marked with the “DELETED” flag in a mailbox or a specific message using the specific_UID attribute.

6.6) Add/Remove/Replace Flags

Adding, removing and replacing one or more flags to messages.

6.6.1) Add Flags

You cannot add a flag that is the antonym of an already existent flag in the message, e.g. cannot add “UNSEEN” flag when a message already has flag “SEEN”. Use replace_flags() instead.

6.7) Move Message(s)

move_msg() uses IMAP “MOVE” EXTENSION. Check if your server supports “MOVE” capability with list_server_capabilities().

If your server does not provide “MOVE” capability, the same result can be achieved with a combination of add_flags() and expunge():

References

Babcock, N., Introduction to IMAP, Blog, May 2016, https://nbsoftsolutions.com/blog/introduction-to-imap.

Crispin, M., INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1, RFC 3501, DOI: 10.17487/RFC3501, March 2003, https://www.rfc-editor.org/info/rfc3501.

Ooms, J. curl: A Modern and Flexible Web Client for R. R package version 3.3, 2019, https://CRAN.R-project.org/package=curl

Stenberg, D. Libcurl - The Multiprotocol File Transfer Library, https://curl.haxx.se/libcurl/

Freed, N. and N. Borenstein, Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types, RFC 2046, DOI: 10.17487/RFC2046, November 1996, https://www.rfc-editor.org/info/rfc2046.

Resnick, P., Ed., Internet Message Format, RFC 2822, DOI: 10.17487/RFC2822, April 2001, https://www.rfc-editor.org/info/rfc2822.

Resnick, P., Ed., Internet Message Format, RFC 5322, DOI: 10.17487/RFC5322, October 2008, https://www.rfc-editor.org/info/rfc5322.