More for you to like:
Closed Captioning Closed captioning available on our YouTube channel
You Might Like

How to send Outlook email, Teams messages and more from R

InfoWorld | Nov 23, 2021

Microsoft’s Azure team has developed Microsoft365R, which makes it possible to send Outlook emails (and Teams messages!) with R. It's a package that interacts with Microsoft 365 APIs. Follow along to learn how to authenticate your account and send messages.

Copyright © 2021 IDG Communications, Inc.

Hi, I’m Sharon Machlis at IDG, here with Episode 66 of Do More With R: Send Outlook email, Teams messages, OneDrive files and more – right from R!
Microsoft’s Azure team has developed Microsoft365R, a package to interact with Microsoft 365 APIs. Let’s see how it works.

Step 1 is authenticating your account. The function you use for that depends on what you want to do. Let’s start with sending an Outlook email message.

To authorize an enterprise Outlook account, use the get_business_outlook() function, and save that to a variable. That variable will be a special R object of class ms_outlook. If you’re using a personal account, the function is get_personal_outlook() instead.

You may run into a problem with your work account and see an error like this. The authorization vignette has some suggestions, which you can see by running the base R vignette function for the package.

The vignette links to some more instructions, with helpful advice. You might want to try again with Microsoft’s official app ID in the argument.

That looks like this – again, that app ID isn’t anything from my setup specifically; it’s Microsoft’s official app ID. And in case anyone from my IT department is watching, this is all theoretical based on official advice from Microsoft’s Azure team! Anyway, if it works, a browser window will pop up asking you to sign in and then authenticate you.

There’s a lot you can do with the Outlook connection, including read and delete emails. But the most interesting one for me is to send email. Start by creating an email object using the create_mail() method of your ms outlook object. The body text of the email is the first argument, and then you can pass other arguments like subject and to: field.

When you do that, a draft email should be saved in your drafts folder.

There are a couple of other syntaxes you can use do to this. In this one, also recommended by the package vignette, you can use the dollar sign symbol almost like a pipe and chain several methods together. This also demonstrations that you can create the email object with content type html and use html formatting in your message, and send to multiple recipients. This also shows up in your Drafts folder.

You can use markdown instead of raw HTML with the blastula package. Here I’m loading the blastula package and then saving some markdown as an R character string, in a variable blastula_body_text. You can turn that string into a blastula email object with blastula’s compose_email() function. The character string goes into the body argument – but wrapped in blastula’s md() function, so compose email knows it’s markdown that needs to be rendered. And then you create an outlook email from the blastula email.

That may be a little confusing (it was to me at first). To recap:

1. Save your markdown in a character string
2. Create a blastula email object with compose_email(), and your string as the first argument wrapped in md()
3. Turn that into an outlook email object with create_email()

There are a few more things you can do with email objects, such as add an attachment with the add_attachment() method. And, of course, send the email with the outlook email object’s send() method. In theory you should also be able to include inline images in your email, but I had mixed success with that so can’t really recommend it yet.

Working with Teams is similar. You authenticate with the get_team() function and the specific name or ID of one of your Teams. Like with Outlook, this may not work in an enterprise. There’s another specific Microsoft app ID – it’s also in the authentication documentation, and I’ve used that in this code. That first argument is my own specific team name or ID. The second argument, app ID, is Microsoft’s; and everyone would use the same one to try this method of authentication.

After you authenticate, create a channel object from the team object with the get_channel() method, like I did here for the General channel in my Team.

It’s pretty simple to send a basic text message to a channel, including an attachment if you want, with the channel object’s send_message() method. This is obviously pretty basic.

As you’d expect, you can run some R code first and put a variable as the send_message argument also.

There are similar authentication and communication functions for OneDrive and SharePoint to list, upload, and download files. I didn’t have too much success authenticating with those at work, but I set up a test personal OneDrive and it was extremely easy to connect.

That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at bit-dot-l-y slash do more with R, all lowercase except for the R. You can also find the Do More With R playlist on YouTube’s IDG Tech Talk channel where you can subscribe so you never miss an episode. Hope to see you next time. Stay healthy and safe, everyone!
Featured videos from