Send Outlook email and Teams messages with R

If you analyze data in R and share information through Microsoft 365, I have good news: there is an easy way to connect R with Outlook, Teams, OneDrive, and SharePoint. The Microsoft365R R package, developed by the Microsoft Azure team, includes functionality to work with the Microsoft 365 R APIs.

Here we will see how to send Outlook emails and Teams messages with R.

Microsoft365R is available from CRAN, so you can install it with install.packages("Microsoft365R").

Authenticate R to work with Outlook

The first step is to authenticate R to work with your account. The role for that depends on the Microsoft 365 service and if you are on a business or personal account.

To authorize a business Outlook account, for example, upload the Microsoft365R package and then use your get_business_outlook() function:

library(Microsoft365R)
my_outlook <- get_business_outlook()

In the code above, I am creating a new object. my_outlook of class ms_outlook.

If you are using a personal Outlook account, use the function get_personal_outlook() instead of.

You may have a problem with your work account and see an error stating that authentication failed “due to policies configured by your administrator.” The Microsoft 365R Authentication Bullet has some suggestions on how to deal with this. You can see the bullet by running the bullet R base function vignette("auth", package = "Microsoft365R").

That bullet links to another page with some helpful tips.

The tip includes a Microsoft365R application ID to use for authentication. Microsoft Azure GitHub repository

Advice on how to troubleshoot Microsoft 365R authentication problems.

The app ID in bold is the official Microsoft app ID. Adding it as an argument to the auth function sometimes helps with approval:

my_outlook <- get_business_outlook(app = "d44a05d5-c6a5-4bbb-82d2-443123722380")

To reiterate: the app ID is nothing of me configuration specifically; is the ID of the official Microsoft application. (And in case someone from your IT department asks about this, you can honestly explain that the advice comes from Microsoft’s own Azure team documentation.)

If authentication works, a browser window will appear asking you to sign in to your Microsoft account and your application will authenticate successfully.

Send Outlook email with R

There are many things you can do with your new Outlook connection, including reading and deleting emails. But the most interesting thing for me is sending an email. To do that, start by creating an email object using the create_mail() method of your ms_outlook object, like:

my_email <- my_outlook$create_email("Body text I want in my email", 
subject = "Email subject", to = "recipient@email.com")

When you run code to create an email object, you must save an email draft in your Outlook Drafts folder.

There are a couple of other syntaxes that you can use to create an email. In the following code, the dollar sign symbol acts as a pipe for chaining together various methods.

my_email <- my_outlook$create_email(content_type = "html")$
set_body("<p>This is my email body <strong>with bold text</strong>.</p>")$
set_subject("My 2nd email subject")$
set_recipients(to = c("recipient1@mail.com", "recipient2@mail.com"))

The above code creates an email object with the HTML content type and uses HTML formatting in the message. It also includes multiple recipients. Creating an email object in this way will also generate a message that will appear in your Outlook Drafts folder.

Outlook email message from Drafts folder Sharon machlis

The email created by R and not yet sent appears in the Drafts folder in Outlook.

You can use Markdown instead of raw HTML in the email body if you pair Microsoft365R with the blastula package. You can do this by first loading the blastula package and then saving some Markdown as an R character string. In the example code below, I save some Markdown in a variable called blastula_body_text.

library(blastula)
blastula_body_text <- "This is my email body with _italics text_. As with usual markdown, **two blank lines** create a new paragraph and two asterisks bold text."

Then convert that markup format string to blastula email object with blastula’s compose_email() function. The character string goes into the body argument, but is wrapped in blastula’s md() work like this compose_email() you know there is markdown in the body that needs to be rendered.

blastula_email <- compose_email(
body = md(blastula_body_text)
)

Finally, you create an Olook email from blastula E-mail:

outlook_email_from_blastula <- my_outlook$create_email(blastula_email, 
subject = "Markdown with blastula", to = "someone@gmail.com")

I found the need for the three steps to be a bit confusing at first. To recap the blastula-to-Outlook workflow:

  1. Save your discount to a string of characters.
  2. Create a blastula email object with compose_email() with your string as the first argument wrapped in md().
  3. Convert blastula email object to Outlook email object with create_email().

There are a few more things you can do with email objects, like add an attachment with the add_attachment() method.

In theory, you should also be able to include online images when sending your email, but I had mixed success with that, so I can’t recommend it yet. ! And at the time of writing this article, even though the Microsoft365R documentation said that it is also compatible with the emayili R email package, I couldn’t get it to work. Update: Both work now, thanks to a review of the Microsoft365R package by package author and maintainer Hong Ooi.

As of this writing, you will need to install the development version of Microsoft365R from GitHub with remotes :: install_github (“Azure / Microsoft365R”); also install emayili from CRAN. Write an R Markdown document as usual, including static charts and graphs (JavaScript-based HTML widgets will not work). Then render the markdown file to an HTML file with rmarkdown :: render ().

Create an emayili mail object with code like

msg <- envelope(
to = "someone@gmail.com",
subject = "My subject",
html = "myfilename.html"
)

where “myfilename.html” is your rendered HTML file.

Then convert it to an Outlook email message:

msg <- outlook$create_email(msg)

Sending email is easy with the Outlook email object send() method for any Outlook email:

msg$send()

Send Teams messages with R

Working with Teams is similar to working with Outlook. Authenticate with the get_team() role, which requires the name or ID of one of your computers. As with Outlook, this may not always work in a business environment. There is another specific Microsoft App ID, also provided in the Microsoft365R Authentication documentation, to test in the Authentication feature if get_team("Team name or ID") it does not work:

my_team <- get_team("Team name or ID", 
app = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"))

The first argument above must be a specific team name or ID that you can access through Teams. The second argument, the application ID, is from Microsoft; everyone would use this same ID to test this authentication method.

After authenticating, create a channel object from the equipment object with the get_channel() method. The following code creates an object for the General channel on my computer.

my_chan <- my_team$get_channel("General")

It’s pretty straightforward to send a basic text message to a channel, including an attachment if you want, using the channel object send_message() method. For instance:

my_chan$send_message("Your upcoming weather forecast", 
attachments = "images/temperature_forecast.png")

There are similar communication and authentication functions for OneDrive and SharePoint that will list, upload, and download files. I wasn’t very successful authenticating with those at work, but it was extremely easy to connect to a personal OneDrive account.

For more information on the Microsoft365R package, go to package repository on Microsoft Azure GitHub. And for more R tips, check out the InfoWorld Do More With R page.

Copyright © 2021 IDG Communications, Inc.

Leave a Comment