Here is a relatively simple way to send HTML emails from R by using Outlook. Besides Microsoft Outlook, there are options not mentioned in this post, but some of the ideas might be useful in those too. HTML emails from R are a great way to share insights by integrating plots or tables in the email body, conclusions in the email subject, etc.
The process of sending emails from R via Outlook is based on the package Microsoft365R. By using this solution, you can send emails without opening Outlook.
First of all, try if you can send emails this way. Go to Microsoft Graph Explorer and test if you can send an email. You can do that by using “send an email” in the Outlook section on the left side panel. If you are getting an error, you probably have to ask the administrator for additional permissions.
Send HTML emails from R by using Outlook
The first step is creating a Microsoft Graph login for the default tenant. After running the get_business_outlook for the first time, you will initiate the authorization process.
require(Microsoft365R) outl <- get_business_outlook() #Using authorization_code flow # Creating Microsoft Graph login for default tenant # Using authorization_code flow # Waiting for authentication in browser... # Press Esc/Ctrl + C to abort
If everything goes without problems, you will see this message in the browser “Authenticated with Azure Active Directory. Please close this page and return to R.”. If you had incorrect authorization, you could clear that like this and start again.
Secondly, create and send a test message using your email address.
outl$create_email("test", subject = "test", to = "[email protected]", content_type = "html")$send()
Here is an example of how to compose and send email message step by step, but I like to create emails as a pipeline.
Here is how to compose and send an HTML email from R that contains multiple email recipients and the hyperlink in the email body.
outl$create_email()$ set_body("That is link <a href = 'https://datacornering.com'>to someting useful!</a> <br></br>", content_type="html")$ set_recipients(to=c("[email protected]", "[email protected]"))$ add_recipients(cc="[email protected]")$ set_subject("Your report is ready!")$ send()
Send R plot in the email body
Try to add an image in the email body containing the R plot. Save the R plot temporarily and then put it in the email.
require(ggplot2) ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) + geom_point() + theme_minimal() tp_path <- tempfile("myplot_", fileext = ".png") ggsave( tp_path, dpi =70, bg = "white" ) outl$create_email()$ set_body("That is link <a href = 'https://datacornering.com'>to someting useful!</a> <br></br>", content_type="html")$ set_recipients(to=c("[email protected]", "[email protected]"))$ add_recipients(cc="[email protected]")$ set_subject("Your report is ready!")$ add_image(tp_path)$ send()
If one plot is not enough, you can combine multiple ones in one image with packages like the patchwork or create small multiples.
Automatization using the R script
If you are a Windows user, look at two posts from this blog that can help you.