Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html table output formatting when sending email from Microsoft Outlook using R

I am trying to convert a dataframe into an html table using the htmlTable package and then send an email using Microsoft Outlook as the email client using RDCOMClient package by appending the html table as the body of the email. I am new to HTML coding so I am not very familiar with how to format the table with HTML tags. Below is an example of what I am trying to do.

# Library to send email from Microsoft Outlook
library(RDCOMClient)
# Library to create HTML table
library(htmlTable)

# Reading data from inbuilt 'mtcars' dataset
x <- head(mtcars)

# Create HTML table
y <- htmlTable(x,
               rnames = FALSE,
               caption="This is from htmlTable package",
               align = paste(rep("c", ncol(x)), collapse = "|"),
               align.header = paste(rep("c", ncol(x)), collapse = "|"))

# add the table as body of the email
body <- paste0("<html>", y, "</html>")

## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email 
outMail = OutApp$CreateItem(0)
## configure  email parameter 
outMail[["To"]] = "test@test"
outMail[["subject"]] = "TEST"
outMail[["HTMLbody"]] = body
## send it                     
outMail$Send()

This above code works and I even get the email output as shown below.

Output from email sent using above code in Microsoft Outlook

image description

My question is, how to I format this table? I want the output to be in a nice table format with row and columns separated by lines. I can add column separator lines as seen in the output, but I am unable to add row separator lines. I also want to adjust the line spacing between the rows and columns, and change the formatting of the fonts to calibri 11. Below is the output that I am looking for.

Desired Output with rows and columns formatted image description

Any help on how I can achieve this using htmlTable package or any other workaround would be really appreciated. Thanks a lot in advance.

UPDATE: Below is the solution, thanks to valuable inputs provided by @Syfer.

library(RDCOMClient)
library(htmlTable)

x <- head(mtcars)
html_y <- htmlTable(x, rnames = FALSE)

body <- paste0("<html><head>
               <style>
               body{font-family:Arial, \"sans-serif\";}
               table{border-left:1px solid #000000;border-top:1px solid #000000;}
               table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:12px; font-weight:normal;}
               table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:14px;}
               </style>
               </head><body>",
               html_y, 
               "</body></html>")

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "[email protected]"
outMail[["subject"]] = "TEST EMAIL"
outMail[["HTMLbody"]] = body
outMail$Send()

Final output in Microsoft Outlook likes this.

Final Output In Microsoft Outlook

like image 545
Code_Sipra Avatar asked Nov 12 '17 12:11

Code_Sipra


People also ask

How do I keep HTML formatting in Outlook?

On the File tab, choose Options > Mail. Under Compose messages, in the Compose messages in this format list, click HTML, Rich Text, or Plain Text.

Why does Outlook mess up HTML?

Outlook versions use the Microsoft Word engine to render HTML emails. Email service providers such as Outlook interpret HTML code in different ways which can cause your email to look different in Outlook than what it looks like in your email marketing platform.


1 Answers

I havent done 'r' but the syntax looks logical so I will take a stab at the solution:

body <- paste0("<html><head><style>body{font-family:Arial, "sans-serif";}table{border-left:1px solid #000000;border-top:1px solid #000000;}table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:normal;}table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:bold;}</style></head><body>", y, "</body></html>")

Basically i have added a few things to your code:

  • head of the html document which contains the style for the HTML document that you will sending out.
  • start and end tag for body.

<style>
    body{font-family:Arial, "sans-serif"}
    table{border-left:1px solid #000000;border-top:1px solid #000000;}
    table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:normal;}
    table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:bold;}
</style>

From the above CSS you should be able to the border colors and fonts to suit your liking. Once the table is rendered in emails it should show something similar to:

enter image description here

Let me know if that worked for you.

Cheers

like image 68
Syfer Avatar answered Sep 26 '22 13:09

Syfer