Send Outlook email and Teams messages with R

If you assess details in R and share information by way of Microsoft 365, I have fantastic news: There is an effortless way to link R with Outlook, Teams, OneDrive, and SharePoint. The Microsoft365R R package, created by the Microsoft Azure team, involves capabilities to work with Microsoft 365 APIs from R. 

In this article we’ll wander via how to mail Outlook email and Teams messages with R.

Microsoft365R is accessible on CRAN, so you can install it with install.offers("Microsoft365R").

Authenticate R to work with Outlook

The first action is to authenticate R to work with your account. The operate for that relies upon on the Microsoft 365 provider and whether it is in a small business or personalized account.

To authorize an company Outlook account, for example, load the Microsoft365R package and then use its get_small business_outlook() operate:

library(Microsoft365R)
my_outlook <- get_business_outlook()

In the code over I am producing a new object my_outlook of class ms_outlook.  

If you’re making use of a personalized Outlook account, use the function get_personalized_outlook() instead. 

You may possibly run into a difficulty with your work account and see an mistake that authentication unsuccessful “due to insurance policies configured by your administrator.” The Microsoft365R authentication vignette has some tips on how to deal with this. You can perspective the vignette by jogging the base R vignette function vignette("auth", package = "Microsoft365R").

That vignette back links to another page with some handy information.

Advice includes a Microsoft365R app ID to use for authentication Microsoft Azure GitHub repo

Guidance on working with Microsoft365R authentication problems.

That application ID in daring is Microsoft’s formal application ID. Incorporating it as an argument to the authentication operate often assists with approval:

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

To reiterate: That application ID is not something from my set up specifically it is Microsoft’s formal application ID. (And in scenario anyone from your IT section asks about this, you can in truth describe that information came from Microsoft’s individual Azure team’s documentation.)

If authentication performs, a browser window will pop up asking you to signal into your Microsoft account, and your application will be productively authenticated.

Deliver Outlook email with R

There is a whole lot you can do with your new Outlook link, together with go through and delete e-mails. But the most fascinating one particular for me is to mail email. To do that, commence by producing an email item making use of the produce_mail() strategy of your ms_outlook item, such as:

my_email <- my_outlook$create_email("Body text I want in my email", 
subject matter = "Electronic mail subject matter", to = "[email protected]")

When you run code producing an email item, a draft email need to be saved in your Outlook Drafts folder.

There are a couple of other syntaxes you can use to produce an email. In the code underneath, the dollar signal symbol acts like a pipe to chain quite a few approaches alongside one another. 

my_email <- my_outlook$create_email(content_type = "html")$
established_system("

This is my email system with daring textual content.

")$
established_subject matter("My 2nd email subject matter")$
established_recipients(to = c("[email protected]", "[email protected]"))

The over code makes an email item with content type HTML and utilizes HTML formatting in the message. It also involves many recipients. Generating an email item this way will also create a message that appears in your Outlook Drafts folder. 

Outlook email message from the Drafts folder Sharon Machlis

Electronic mail created by R and not nevertheless sent appears in your Outlook Drafts folder.

You can use Markdown rather of raw HTML in the email system if you pair Microsoft365R with the blastula package. You can do this by first loading the blastula package and then preserving some Markdown as an R character string. In the example code underneath, I help save some Markdown to a variable called blastula_system_textual content.

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

Following, transform that character string with markdown formatting into a blastula email item with blastula’s compose_email() operate. The character string goes into the system argument, but it is wrapped in blastula’s md() operate so compose_email() appreciates there is Markdown in the system that requirements to be rendered.

blastula_email <- compose_email(
system = md(blastula_system_textual content)
)

At last, you produce an Outlook email from the blastula email:

outlook_email_from_blastula <- my_outlook$create_email(blastula_email, 
subject matter = "Markdown with blastula", to = "[email protected]")

I located the need to have for all a few measures to be a little bit puzzling at first. To recap the blastula-to-Outlook workflow:

  1. Help you save your markdown in a character string.
  2. Produce a blastula email item with compose_email() with your string as the first argument wrapped in md().
  3. Transform the blastula email item into an Outlook email item with produce_email().

There are a several extra issues you can do with email objects, such as include an attachment with the include_attachment() strategy.

In theory, you need to also be ready to contain inline illustrations or photos when sending your email, but I had combined success with that so just can't suggest it nevertheless. And at the time of this creating, even though the Microsoft365R documentation claimed it is also compatible with the emayili R email package, I could not get it to work (and emayili's creator claimed it appears Microsoft365R was making use of an old variation of the emayili API).

Sending email is effortless with the Outlook email object’s mail() strategy:

my_email$mail()

Deliver Teams messages with R

Operating with Teams is comparable to doing the job with Outlook. Authenticate with the get_team() operate, which needs the title or ID of one particular of your Teams. As with Outlook, this may possibly not often work in an company atmosphere. There is another precise Microsoft application ID — also presented in the Microsoft365R authentication documentation — to consider in the authentication operate if get_team("Workforce title or ID") does not work: 

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

That first argument over need to be a precise team title or ID you can entry by way of Teams. The second argument, application ID, is Microsoft’s every person would use this identical ID to consider this strategy of authentication.

Following you authenticate, produce a channel item from the team item with the get_channel() strategy. The code underneath makes an item for the Typical channel in my Workforce.

my_chan <- my_team$get_channel("General")

It is pretty simple to mail a fundamental textual content message to a channel, together with an attachment if preferred, making use of the channel object’s mail_message() strategy. For example:

my_chan$mail_message("Your upcoming weather conditions forecast", 
attachments = "illustrations or photos/temperature_forecast.png")

There are comparable authentication and interaction capabilities for OneDrive and SharePoint that will list, add, and download documents. I did not have as well a lot success authenticating with all those at work, but it was exceptionally effortless to link to a personalized OneDrive account.

For extra information on the Microsoft365R package, head to the package repository in Microsoft Azure’s GitHub. And for extra R recommendations, check out the InfoWorld Do More With R page.

Copyright © 2021 IDG Communications, Inc.