Learn R Programming

⚠️There's a newer version (1.1.4) of this package.Take me there.

mRpostman

IMAP Toolkit for R

Overview

mRpostman makes extensive use of ‘curl’ and ‘libcurl’ capabilities, providing functions for mailboxes and electronic messages manipulation. Beyond many other functionalities, it is possible to move, delete, search and fetch messages or its parts using specific criteria.

mRpostman website: https://allanvc.github.io/mRpostman

First things first …

Before using mRpostman, it is essential to configure your email account. Many mail providers 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

Outlook - Office 365

There is no need to execute any external configuration. The parameter url in configure_imap() should be set as url="imaps://outlook.office365.com".

Introduction

The package is divided in 8 groups of functions:

  • configuration: configure_imap();
  • mailboxes commands: list_mailboxes(), select_mailbox(), examine_mailbox(), rename_mailbox();
  • options listing: list_server_capabilities(),flag_options(), section_or_field_options(), metadata_options();
  • search: search_before(), search_since(), search_period(), search_on(), search_sent_before(),search_sent_since(), search_sent_period(), search_sent_on(), search_string(), search_flag(), search_smaller_than(), search_larger_than(), search_younger_than(), search_older_than(), custom_search();
  • custom search helper functions:
    • relational operators functions: AND(), OR();
    • criteria definition functions: before(), since(), on(), sent_before(), sent_since(), sent_on(), string(), flag(), smaller_than(), larger_than(), younger_than(), older_than();
  • fetch: fetch_full_msg(), fetch_msg_header(), fetch_msg_text(), fetch_msg_metadata();
  • attachments: list_attachments(), get_attachments();
  • miscellania: copy_msg(), get_min_id(), get_max_id(), delete_msg(), expunge(), add_flags(), remove_flags(), replace_flags(), move_msg().

Installation

# CRAN version
install.packages("mRpostman")

# Dev version
if (!require('remotes')) install.packages('remotes')
remotes::install_github("allanvc/mRpostman")

Basic Usage

1) Configuring IMAP and listing mailboxes


library(mRpostman)

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

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

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

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

# Outlook - Office 365
# imapconf <- configure_imap(url="imaps://outlook.office365.com",
#                            username="your_user",
#                            password=rstudioapi::askForPassword()
#                           )

# you can try another IMAP server and see if it works

# Listing
imapconf %>%
  list_mailboxes()

2) Examining a Mailbox


# examine mailbox -- number of existent and recent messages
imapconf %>%
  select_mailbox(mbox = "Kansas State University") %>% # mbox names are case sensitive
  examine_mailbox()

3) Searching by period using a flag


# search
results <- imapconf %>%
  select_mailbox(mbox = "INBOX") %>%
  search_period(since_date_char = "17-May-2018",
                before_date_char = "30-Jun-2019",
                flag = "ANSWERED")

results$msg_id

4) Searching for a string in the “Text” section of messages


# search
results <- imapconf %>%
  select_mailbox(mbox = "Kansas State University") %>%
  search_string(section_or_field = "TEXT", string = "Welcome!")

results$msg_id

5) Fetch headers after searching


results <- imapconf %>%
  select_mailbox(mbox = "Kansas State University") %>%
  search_string(section_or_field = "TEXT", string = "Welcome!") %$% # exposition pipe, not %>%!!
  fetch_msg_header(imapconf = imapconf, 
                   msg_id = msg_id, 
                   fields = c("DATE", "SUBJECT"))

results

6) Attachments

This will list all the attachments filenames and the Content-Disposition type for each fetched message.

imapconf %>%
  select_mailbox(mbox = "INBOX") %>%
  search_since(date_char = "23-Sep-2019") %$%
  fetch_full_msg(imapconf, msg_id=msg_id) %>%
  list_attachments()

This extracts and decode your base64 text attachments (regular and inline) from messages, and save them locally.

imapconf %>%
  select_mailbox(mbox = "INBOX") %>%
  search_since(date_char = "23-Sep-2019") %$%
  fetch_full_msg(imapconf, msg_id=msg_id) %>%
  get_attachments()

Future Improvements

  • better error handling and messages;

  • add further IMAP functionalities;

  • include SMTP support for sending emails.

If you want to contribute, specially with the SMTP support, please contact the package mantainer.

License

This package is licensed under the terms of the GPL-3 License.

References

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/

Copy Link

Version

Install

install.packages('mRpostman')

Monthly Downloads

293

Version

0.3.1

License

GPL-3

Maintainer

Allan Quadros

Last Published

April 20th, 2020

Functions in mRpostman (0.3.1)

AND

Relational Operators - Helper Functions for Custom Search
check_args_custom_search

Custom Search Arguments Check
OR

Relational Operators - Helper Functions for Custom Search
check_args_expunge

Expunge Message Arguments Check
check_args_date

Custom Search Specific Check
check_args_copy_msg

Copy Message Arguments Check
check_args_delete_msg

Delete Message Arguments Check
check_args_fetch_full_msg

Full Fetch Arguments Check
check_args_add_replace_flags

Add/Replace Flags Arguments Check
check_args_fetch_msg_text

Text Fetch Arguments Check
check_args_move_msg

Move Message Arguments Check
check_args_get_max_min_id

Max/Min Message Arguments Check
check_args_fetch_msg_metadata

Metadata Fetch Arguments Check
check_args_flag

Custom Search Specific Check
check_args_get_attachments

Get Attachments Arguments Check
check_args_remove_flags

Remove Flags Arguments Check
check_args_search_date

Date Search Arguments Check
check_args_list_attachments

List Attachments Arguments Check
check_args_search_size

Size Search Arguments Check
define_searchrequest_flag

Flag Search Request
define_searchrequest_period

Period Search Request
check_args_search_period

Period Search Arguments Check
custom_search

Custom Search
check_args_within

Custom Search Specific Check
decode_base64_text_if_needed

Messages Results Decoding
check_args_search_flag

Flag Search Arguments Check
check_args_size

Custom Search Specific Check
define_searchrequest_within

Within Search Request
check_args_fetch_msg_header

Header Fetch Arguments Check
delete_msg

Delete Messages
configure_imap

IMAP Settings
clean_fetch_results

Messages Results Cleaning
define_searchrequest_size

Size Search Request
check_args_string

Custom Search Specific Check
fetch_msg_metadata

Fetch Message Metadata
fetch_msg_header

Fetch Message Headers
copy_msg

Copy Messages
check_args_search_string

String Search Arguments Check
fetch_msg_text

Fetch Message Text
check_args_search_within

Within Search Arguments Check
config_handle

Curl Handle Configuration
expunge

Expunge Messages
get_min_id

Get Minimum Message ID in a Mailbox
define_searchrequest_string

String Search Request
list_mailboxes

Listing Mailboxes
larger_than

Criteria Helper Functions for Custom Search
fetch_full_msg

Fetch Full Messages
define_searchrequest_date

Date Search Request
define_searchrequest_custom

Custom Search Request
get_attachments

Get Attachments
list_attachments

List Attachments
older_than

Criteria Helper Functions for Custom Search
move_msg

Move Messages
has_attachment

Attachments Check
search_on

Search By Internal Date
search_older_than

Search Within seconds
fix_search_stripping

Fixing Stripped Search Results
get_max_id

Get Maximum Message ID in a Mailbox
examine_mailbox

Examine Mailbox
count_msgs

Count the Number of Messages
%$%

Exposition Pipe operator
flag

Criteria Helper Functions for Custom Search
loop_fetch_msg_text

Metadata Fetch Loop
flag_options

Flag Options
mRpostman-package

IMAP Tools for R
list_server_capabilities

IMAP Server Capabilities
search_smaller_than

Search By Syze
loop_fetch_msg_metadata

Metadata Fetch Loop
metadata_options

Message Metadata Options
search_flag

Search By Flag
search_string

Search By String
replace_flags

Replace Flag(s) in Messages
loop_fetch_msg_header

Header Fetch Loop
remove_flags

Remove Flag(s) From Messages
rename_mailbox

Rename Mailbox
search_period

Search By Internal Date
on

Criteria Helper Functions for Custom Search
loop_fetch_full_msg

Full Fetch Loop
search_sent_on

Search By Origination (RC-2822 Header) Date
select_mailbox

Mailbox Selection
sent_before

Criteria Helper Functions for Custom Search
%>%

Common Pipe operator
search_larger_than

Search By Syze
sent_on

Criteria Helper Functions for Custom Search
search_sent_period

Search By Origination (RC-2822 Header) Date
sent_since

Criteria Helper Functions for Custom Search
string

Criteria Helper Functions for Custom Search
search_sent_before

Search By Origination (RC-2822 Header) Date
search_younger_than

Search Within seconds
younger_than

Criteria Helper Functions for Custom Search
search_before

Search By Internal Date
search_since

Search By Internal Date
search_sent_since

Search By Origination (RC-2822 Header) Date
since

Criteria Helper Functions for Custom Search
section_or_field_options

Section or Header Fields Options
smaller_than

Criteria Helper Functions for Custom Search
before

Criteria Helper Functions for Custom Search
add_flags

Add Flag(s) To Messages