Licensing Consultant

Not just any technology

Send Outlook email and Teams messages with R

If you evaluate data in R and share facts by using Microsoft 365, I have superior information: There is an quick way to hook up R with Outlook, Teams, OneDrive, and SharePoint. The Microsoft365R R package deal, made by the Microsoft Azure workforce, involves features to get the job done with Microsoft 365 APIs from R. 

Below we’ll stroll via how to ship Outlook e mail and Teams messages with R.

Microsoft365R is offered on CRAN, so you can install it with"Microsoft365R").

Authenticate R to get the job done with Outlook

The initially phase is to authenticate R to get the job done with your account. The purpose for that is dependent on the Microsoft 365 support and regardless of whether it is in a business enterprise or particular account.

To authorize an company Outlook account, for case in point, load the Microsoft365R package deal and then use its get_business enterprise_outlook() purpose:

my_outlook <- get_business_outlook()

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

If you’re employing a particular Outlook account, use the function get_particular_outlook() instead. 

You may perhaps operate into a challenge with your get the job done account and see an mistake that authentication unsuccessful “due to policies configured by your administrator.” The Microsoft365R authentication vignette has some solutions on how to offer with this. You can see the vignette by working the foundation R vignette function vignette("auth", package deal = "Microsoft365R").

That vignette backlinks to one more site with some valuable guidance.

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

Advice on working with Microsoft365R authentication difficulties.

That application ID in bold is Microsoft’s formal application ID. Adding it as an argument to the authentication purpose in some cases aids with approval:

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

To reiterate: That application ID isn’t just about anything from my setup precisely it is Microsoft’s formal application ID. (And in scenario any one from your IT division asks about this, you can truthfully clarify that guidance arrived from Microsoft’s personal Azure team’s documentation.)

If authentication works, a browser window will pop up inquiring you to indication into your Microsoft account, and your application will be successfully authenticated.

Deliver Outlook e mail with R

There’s a ton you can do with your new Outlook relationship, together with browse and delete email messages. But the most attention-grabbing one particular for me is to ship e mail. To do that, get started by making an e mail item employing the develop_mail() strategy of your ms_outlook item, this kind of as:

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

When you operate code making an e mail item, a draft e mail really should be saved in your Outlook Drafts folder.

There are a pair of other syntaxes you can use to develop an e mail. In the code under, the greenback indication image acts like a pipe to chain numerous approaches together. 

my_e mail <- my_outlook$create_email(content_type = "html")$
established_entire body("

This is my e mail entire body with bold text.

established_issue("My 2nd e mail issue")$
established_recipients(to = c("[email protected]", "[email protected]"))

The above code generates an e mail item with content material style HTML and works by using HTML formatting in the concept. It also involves numerous recipients. Building an e mail item this way will also deliver a concept that appears in your Outlook Drafts folder. 

Outlook email message from the Drafts folder Sharon Machlis

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

You can use Markdown as an alternative of uncooked HTML in the e mail entire body if you pair Microsoft365R with the blastula package deal. You can do this by initially loading the blastula package deal and then saving some Markdown as an R character string. In the case in point code under, I help save some Markdown to a variable called blastula_entire body_text.

blastula_entire 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."

Future, convert that character string with markdown formatting into a blastula e mail item with blastula’s compose_e mail() purpose. The character string goes into the entire body argument, but it is wrapped in blastula’s md() purpose so compose_e mail() is aware of there is Markdown in the entire body that requires to be rendered.

blastula_e mail <- compose_email(
entire body = md(blastula_entire body_text)

Eventually, you develop an Outlook e mail from the blastula e mail:

outlook_e mail_from_blastula <- my_outlook$create_email(blastula_email, 
issue = "Markdown with blastula", to = "[email protected]")

I observed the have to have for all 3 ways to be a little bit perplexing at initially. To recap the blastula-to-Outlook workflow:

  1. Help you save your markdown in a character string.
  2. Create a blastula e mail item with compose_e mail() with your string as the initially argument wrapped in md().
  3. Flip the blastula e mail item into an Outlook e mail item with develop_e mail().

There are a number of a lot more items you can do with e mail objects, this kind of as increase an attachment with the increase_attachment() strategy.

In idea, you really should also be equipped to involve inline photographs when sending your e mail, but I experienced combined results with that so can not endorse it but. And at the time of this composing, while the Microsoft365R documentation said it is also appropriate with the emayili R e mail package deal, I could not get it to get the job done (and emayili's creator said it appears Microsoft365R was employing an old model of the emayili API).

Sending e mail is quick with the Outlook e mail object’s ship() strategy:

my_e mail$ship()

Deliver Teams messages with R

Working with Teams is comparable to functioning with Outlook. Authenticate with the get_workforce() purpose, which necessitates the title or ID of one particular of your Teams. As with Outlook, this may perhaps not generally get the job done in an company surroundings. There is one more specific Microsoft application ID — also delivered in the Microsoft365R authentication documentation — to check out in the authentication purpose if get_workforce("Group title or ID") does not get the job done: 

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

That initially argument above really should be a specific workforce title or ID you can access by using Teams. The second argument, application ID, is Microsoft’s every person would use this exact same ID to check out this strategy of authentication.

Soon after you authenticate, develop a channel item from the workforce item with the get_channel() strategy. The code under generates an item for the Standard channel in my Group.

my_chan <- my_team$get_channel("General")

It’s fairly very simple to ship a primary text concept to a channel, together with an attachment if wished-for, employing the channel object’s ship_concept() strategy. For case in point:

my_chan$ship_concept("Your approaching weather conditions forecast", 
attachments = "photographs/temperature_forecast.png")

There are comparable authentication and communication features for OneDrive and SharePoint that will listing, add, and download files. I did not have far too a great deal results authenticating with these at get the job done, but it was incredibly quick to hook up to a particular OneDrive account.

For a lot more facts on the Microsoft365R package deal, head to the package deal repository in Microsoft Azure’s GitHub. And for a lot more R guidelines, test out the InfoWorld Do Far more With R site.

Copyright © 2021 IDG Communications, Inc.