Send Outlook email and Teams messages with R

If you assess knowledge in R and share details through Microsoft 365, I have fantastic information: There is an straightforward way to link R with Outlook, Groups, OneDrive, and SharePoint. The Microsoft365R R bundle, made by the Microsoft Azure staff, incorporates functions to work with Microsoft 365 APIs from R. 

Right here we’ll walk by means of how to send out Outlook e mail and Groups messages with R.

Microsoft365R is available on CRAN, so you can set up it with set up.offers("Microsoft365R").

Authenticate R to work with Outlook

The very first phase is to authenticate R to work with your account. The functionality for that relies upon on the Microsoft 365 support and no matter whether it’s in a business enterprise or particular account.

To authorize an enterprise Outlook account, for instance, load the Microsoft365R bundle and then use its get_business enterprise_outlook() functionality:

library(Microsoft365R)
my_outlook <- get_business_outlook()

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

If you are using a particular Outlook account, use the function get_particular_outlook() instead. 

You may operate into a dilemma with your work account and see an error that authentication failed “due to guidelines configured by your administrator.” The Microsoft365R authentication vignette has some strategies on how to deal with this. You can look at the vignette by operating the foundation R vignette function vignette("auth", bundle = "Microsoft365R").

That vignette inbound links to a different website page with some beneficial assistance.

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

Tips on working with Microsoft365R authentication difficulties.

That application ID in daring is Microsoft’s official application ID. Including it as an argument to the authentication functionality from time to time allows with acceptance:

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

To reiterate: That application ID is not nearly anything from my setup especially it’s Microsoft’s official application ID. (And in case everyone from your IT section asks about this, you can honestly make clear that assistance arrived from Microsoft’s own Azure team’s documentation.)

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

Ship Outlook e mail with R

There is a whole lot you can do with your new Outlook connection, such as examine and delete email messages. But the most intriguing a single for me is to send out e mail. To do that, start out by making an e mail item using the develop_mail() method of your ms_outlook item, these kinds of as:

my_e mail <- my_outlook$create_email("Body text I want in my email", 
subject = "E mail subject", to = "[email protected] mail.com")

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

There are a couple 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 many strategies collectively. 

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

This is my e mail overall body with daring textual content.

")$
set_subject("My 2nd e mail subject")$
set_recipients(to = c("[email protected]", "[email protected]"))

The above code produces an e mail item with material form HTML and uses HTML formatting in the concept. It also incorporates a number of recipients. Making an e mail item this way will also make a concept that seems in your Outlook Drafts folder. 

Outlook email message from the Drafts folder Sharon Machlis

E mail designed by R and not nevertheless sent seems in your Outlook Drafts folder.

You can use Markdown rather of raw HTML in the e mail overall body if you pair Microsoft365R with the blastula bundle. You can do this by very first loading the blastula bundle and then conserving some Markdown as an R character string. In the instance code under, I help you save some Markdown to a variable called blastula_overall body_textual content.

library(blastula)
blastula_overall body_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."

Upcoming, turn that character string with markdown formatting into a blastula e mail item with blastula’s compose_e mail() functionality. The character string goes into the overall body argument, but it’s wrapped in blastula’s md() functionality so compose_e mail() is aware there is Markdown in the overall body that wants to be rendered.

blastula_e mail <- compose_email(
overall body = md(blastula_overall body_textual content)
)

At last, you develop an Outlook e mail from the blastula e mail:

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

I uncovered the will need for all 3 methods to be a little bit complicated at very first. To recap the blastula-to-Outlook workflow:

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

There are a several far more things you can do with e mail objects, these kinds of as add an attachment with the add_attachment() method.

In theory, you should also be capable to involve inline visuals when sending your e mail, but I had combined results with that so can not advise it nevertheless. ! And at the time of this creating, although the Microsoft365R documentation explained it is also suitable with the emayili R e mail bundle, I couldn't get it to work. Update: The two work now, many thanks to a Microsoft365R bundle revision by bundle writer and maintainer Hong Ooi. 

As of this creating, you can expect to will need to set up the progress variation of Microsoft365R from GitHub with remotes::set up_github("Azure/Microsoft365R") also set up emayili from CRAN. Compose an R Markdown document as normal such as static plots and graphs (JavaScript-primarily based HTML widgets will not work). Then render the markdown file to an HTML file with rmarkdown::render(). 

Create an emayili mail item with code these kinds of as

msg <- envelope(
to = "an [email protected]",
subject = "My subject",
html = "myfilename.html"
)

the place "myfilename.html" is your rendered HTML file.

Then turn that into an outlook e mail concept:

msg <- outlook$create_email(msg)

Sending e mail is straightforward with the Outlook e mail object’s send out() method for any outlook e mail:

msg$send out()

Ship Groups messages with R

Functioning with Groups is related to doing the job with Outlook. Authenticate with the get_staff() functionality, which needs the name or ID of a single of your Groups. As with Outlook, this may not usually work in an enterprise atmosphere. There is a different certain Microsoft application ID — also offered in the Microsoft365R authentication documentation — to attempt in the authentication functionality if get_staff("Workforce name or ID") doesn’t work: 

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

That very first argument above should be a certain staff name or ID you can entry through Groups. The next argument, application ID, is Microsoft’s all people would use this exact same ID to attempt this method of authentication.

After you authenticate, develop a channel item from the staff item with the get_channel() method. The code under produces an item for the Standard channel in my Workforce.

my_chan <- my_team$get_channel("General")

It’s pretty easy to send out a primary textual content concept to a channel, such as an attachment if ideal, using the channel object’s send out_concept() method. For instance:

my_chan$send out_concept("Your forthcoming weather forecast", 
attachments = "visuals/temperature_forecast.png")

There are related authentication and communication functions for OneDrive and SharePoint that will record, upload, and obtain documents. I didn’t have also much results authenticating with people at work, but it was incredibly straightforward to link to a particular OneDrive account.

For far more details on the Microsoft365R bundle, head to the bundle repository in Microsoft Azure’s GitHub. And for far more R ideas, test out the InfoWorld Do Much more With R website page.

Copyright © 2021 IDG Communications, Inc.

Next Post

Oppo Reno 7 5G, Reno 7 Pro 5G, Reno 7 SE 5G With Triple Rear Cameras Launched: Price, Specifications

Oppo Reno seven series was released on Thursday. The new series comprises the Oppo Reno seven 5G as perfectly as the Oppo Reno seven Pro 5G and Reno seven SE 5G. All a few new Oppo Reno phones come with an AMOLED show with a hole-punch style and aspect a […]