Sending HTML reports inline and as attachment created with knitr

This file shows a typical workflow for knitting .Rmd documents and sending them per E-Mail.

First we have our my_file.Rmd which looks like this:

---
title: ' '
output: 
  html_document:
    theme: null
    highlight: null
    mathjax: null
---

Hello everyone,\n

here is a calculation.

**2+2 =**

`​``{r echo=FALSE}
2+2
`​``

All the best

Render your rmarkdown file

htmlout <- tempfile(fileext = ".html")

rmarkdown::render(
      input = "my_file.Rmd",
      intermediates_dir = ".",
      output_file = htmlout,
    )

This is the resulting HTML document

Hello everyone,

here is a calculation.

2+2 =

#> [1] 4

All the best


Sending the html file per E-Mail

We can now send the the resulting html file as A) an file attachment or B) inline HTML.

A) File attachment

library(sendmailR)
sendmail(from="[email protected]",
         to="[email protected]",
         subject="File attachment",
         msg=c(
           mime_part("Hello everyone,\n here is the newest report.\n Bye"),
           mime_part(htmlout, name = "report.html")),
         engine = "debug")
#>  [1] "From: [email protected]"                                                                                      
#>  [2] "To: [email protected]"                                                                                         
#>  [3] "Subject: File attachment"                                                                                    
#>  [4] "Date: Sat, 02 Nov 2024 02:56:26 -0000"                                                                       
#>  [5] "MIME-Version: 1.0"                                                                                           
#>  [6] "Content-Type: multipart/mixed; boundary=\"dcfc8f0a4bfc223e2cd6f7da4f53877d55e2c16099d930e128cd99681845529b\""
#>  [7] ""                                                                                                            
#>  [8] "This is a message with multiple parts in MIME format."                                                       
#>  [9] "--dcfc8f0a4bfc223e2cd6f7da4f53877d55e2c16099d930e128cd99681845529b"                                          
#> [10] "Content-Type: text/plain; charset=utf-8"                                                                     
#> [11] "Content-Disposition: inline"                                                                                 
#> [12] ""                                                                                                            
#> [13] "Hello everyone,"                                                                                             
#> [14] " here is the newest report."                                                                                 
#> [15] " Bye"                                                                                                        
#> [16] "--dcfc8f0a4bfc223e2cd6f7da4f53877d55e2c16099d930e128cd99681845529b"                                          
#> [17] "Content-Type: application/octet-stream"                                                                      
#> [18] "Content-Disposition: attachment; filename=report.html"                                                       
#> [19] "Content-Transfer-Encoding: base64"                                                                           
#> [20] ""                                                                                                            
#> [21] "PCFET0NUWVBFIGh0bWw+Cgo8aHRtbD4KCjxoZWFkPgoKPG1ldGEgY2hhcnNldD0idXRmLTgi"                                    
#> [22] "IC8+CjxtZXRhIG5hbWU9ImdlbmVyYXRvciIgY29udGVudD0icGFuZG9jIiAvPgo8bWV0YSBo"                                    
#> [23] "dHRwLWVxdWl2PSJYLVVBLUNvbXBhdGlibGUiIGNvbnRlbnQ9IklFPUVER0UiIC8+Cgo8bWV0"                                    
#> [24] "YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLCBpbml0aWFs"                                    
#> [25] "LXNjYWxlPTEiIC8+CgoKCjx0aXRsZT4gPC90aXRsZT4KCjxzY3JpcHQ+Ly8gUGFuZG9jIDIu"                                    
#> [26] "OSBhZGRzIGF0dHJpYnV0ZXMgb24gYm90aCBoZWFkZXIgYW5kIGRpdi4gV2UgcmVtb3ZlIHRo"                                    
#> [27] "ZSBmb3JtZXIgKHRvCi8vIGJlIGNvbXBhdGlibGUgd2l0aCB0aGUgYmVoYXZpb3Igb2YgUGFu"                                    
#> [28] "ZG9jIDwgMi44KS4KZG9jdW1lbnQuYWRkRXZlbnRMaXN0ZW5lcignRE9NQ29udGVudExvYWRl"                                    
#> [29] "ZCcsIGZ1bmN0aW9uKGUpIHsKICB2YXIgaHMgPSBkb2N1bWVudC5xdWVyeVNlbGVjdG9yQWxs"                                    
#> [30] "KCJkaXYuc2VjdGlvbltjbGFzcyo9J2xldmVsJ10gPiA6Zmlyc3QtY2hpbGQiKTsKICB2YXIg"                                    
#> [31] "aSwgaCwgYTsKICBmb3IgKGkgPSAwOyBpIDwgaHMubGVuZ3RoOyBpKyspIHsKICAgIGggPSBo"                                    
#> [32] "c1tpXTsKICAgIGlmICghL15oWzEtNl0kL2kudGVzdChoLnRhZ05hbWUpKSBjb250aW51ZTsg"                                    
#> [33] "IC8vIGl0IHNob3VsZCBiZSBhIGhlYWRlciBoMS1oNgogICAgYSA9IGguYXR0cmlidXRlczsK"                                    
#> [34] "ICAgIHdoaWxlIChhLmxlbmd0aCA+IDApIGgucmVtb3ZlQXR0cmlidXRlKGFbMF0ubmFtZSk7"                                    
#> [35] "CiAgfQp9KTsKPC9zY3JpcHQ+Cgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgpjb2Rle3doaXRl"                                    
#> [36] "LXNwYWNlOiBwcmUtd3JhcDt9CnNwYW4uc21hbGxjYXBze2ZvbnQtdmFyaWFudDogc21hbGwt"                                    
#> [37] "Y2Fwczt9CnNwYW4udW5kZXJsaW5le3RleHQtZGVjb3JhdGlvbjogdW5kZXJsaW5lO30KZGl2"                                    
#> [38] "LmNvbHVtbntkaXNwbGF5OiBpbmxpbmUtYmxvY2s7IHZlcnRpY2FsLWFsaWduOiB0b3A7IHdp"                                    
#> [39] "ZHRoOiA1MCU7fQpkaXYuaGFuZ2luZy1pbmRlbnR7bWFyZ2luLWxlZnQ6IDEuNWVtOyB0ZXh0"                                    
#> [40] "LWluZGVudDogLTEuNWVtO30KdWwudGFzay1saXN0e2xpc3Qtc3R5bGU6IG5vbmU7fQouZGlz"                                    
#> [41] "cGxheS5tYXRoe2Rpc3BsYXk6IGJsb2NrOyB0ZXh0LWFsaWduOiBjZW50ZXI7IG1hcmdpbjog"                                    
#> [42] "MC41cmVtIGF1dG87fQo8L3N0eWxlPgoKCgoKCgoKCgoKCjwvaGVhZD4KCjxib2R5PgoKCgoK"                                    
#> [43] "PGgxIGNsYXNzPSJ0aXRsZSB0b2MtaWdub3JlIj4gPC9oMT4KCgoKPHA+SGVsbG8gZXZlcnlv"                                    
#> [44] "bmUsPC9wPgo8cD5oZXJlIGlzIGEgY2FsY3VsYXRpb24uPC9wPgo8cD48c3Ryb25nPjIrMiA9"                                    
#> [45] "PC9zdHJvbmc+PC9wPgo8cHJlPjxjb2RlPiMmZ3Q7IFsxXSA0PC9jb2RlPjwvcHJlPgo8cD5B"                                    
#> [46] "bGwgdGhlIGJlc3Q8L3A+CgoKCjwhLS0gY29kZSBmb2xkaW5nIC0tPgoKCgo8L2JvZHk+Cjwv"                                    
#> [47] "aHRtbD4K"                                                                                                    
#> [48] "--dcfc8f0a4bfc223e2cd6f7da4f53877d55e2c16099d930e128cd99681845529b--"

B) Inline HTML

sendmail(from="[email protected]",
       to="[email protected]",
       subject="Inline HTML",
       msg=mime_part_html(htmlout),
       engine = "debug")
#> Recipients: [email protected]
#>  [1] "From: [email protected]"                                                                                      
#>  [2] "To: [email protected]"                                                                                         
#>  [3] "Subject: Inline HTML"                                                                                        
#>  [4] "Date: Sat, 02 Nov 2024 02:56:26 -0000"                                                                       
#>  [5] "MIME-Version: 1.0"                                                                                           
#>  [6] "Content-Type: multipart/mixed; boundary=\"ca343ffac9750dc5ef8b589d555af0b085a9715d1f5cf9a562e8b3d77cca4854\""
#>  [7] ""                                                                                                            
#>  [8] "This is a message with multiple parts in MIME format."                                                       
#>  [9] "--ca343ffac9750dc5ef8b589d555af0b085a9715d1f5cf9a562e8b3d77cca4854"                                          
#> [10] "Content-Type: text/html; charset=utf-8"                                                                      
#> [11] "Content-Disposition: inline"                                                                                 
#> [12] ""                                                                                                            
#> [13] "<!DOCTYPE html>"                                                                                             
#> [14] ""                                                                                                            
#> [15] "<html>"                                                                                                      
#> [16] ""                                                                                                            
#> [17] "<head>"                                                                                                      
#> [18] ""                                                                                                            
#> [19] "<meta charset=\"utf-8\" />"                                                                                  
#> [20] "<meta name=\"generator\" content=\"pandoc\" />"                                                              
#> [21] "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EDGE\" />"                                                 
#> [22] ""                                                                                                            
#> [23] "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />"                                  
#> [24] ""                                                                                                            
#> [25] ""                                                                                                            
#> [26] ""                                                                                                            
#> [27] "<title> </title>"                                                                                            
#> [28] ""                                                                                                            
#> [29] "<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to"                      
#> [30] "// be compatible with the behavior of Pandoc < 2.8)."                                                        
#> [31] "document.addEventListener('DOMContentLoaded', function(e) {"                                                 
#> [32] "  var hs = document.querySelectorAll(\"div.section[class*='level'] > :first-child\");"                       
#> [33] "  var i, h, a;"                                                                                              
#> [34] "  for (i = 0; i < hs.length; i++) {"                                                                         
#> [35] "    h = hs[i];"                                                                                              
#> [36] "    if (!/^h[1-6]$/i.test(h.tagName)) continue;  // it should be a header h1-h6"                             
#> [37] "    a = h.attributes;"                                                                                       
#> [38] "    while (a.length > 0) h.removeAttribute(a[0].name);"                                                      
#> [39] "  }"                                                                                                         
#> [40] "});"                                                                                                         
#> [41] "</script>"                                                                                                   
#> [42] ""                                                                                                            
#> [43] "<style type=\"text/css\">"                                                                                   
#> [44] "code{white-space: pre-wrap;}"                                                                                
#> [45] "span.smallcaps{font-variant: small-caps;}"                                                                   
#> [46] "span.underline{text-decoration: underline;}"                                                                 
#> [47] "div.column{display: inline-block; vertical-align: top; width: 50%;}"                                         
#> [48] "div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}"                                                
#> [49] "ul.task-list{list-style: none;}"                                                                             
#> [50] ".display.math{display: block; text-align: center; margin: 0.5rem auto;}"                                     
#> [51] "</style>"                                                                                                    
#> [52] ""                                                                                                            
#> [53] ""                                                                                                            
#> [54] ""                                                                                                            
#> [55] ""                                                                                                            
#> [56] ""                                                                                                            
#> [57] ""                                                                                                            
#> [58] ""                                                                                                            
#> [59] ""                                                                                                            
#> [60] ""                                                                                                            
#> [61] ""                                                                                                            
#> [62] ""                                                                                                            
#> [63] "</head>"                                                                                                     
#> [64] ""                                                                                                            
#> [65] "<body>"                                                                                                      
#> [66] ""                                                                                                            
#> [67] ""                                                                                                            
#> [68] ""                                                                                                            
#> [69] ""                                                                                                            
#> [70] "<h1 class=\"title toc-ignore\"> </h1>"                                                                       
#> [71] ""                                                                                                            
#> [72] ""                                                                                                            
#> [73] ""                                                                                                            
#> [74] "<p>Hello everyone,</p>"                                                                                      
#> [75] "<p>here is a calculation.</p>"                                                                               
#> [76] "<p><strong>2+2 =</strong></p>"                                                                               
#> [77] "<pre><code>#&gt; [1] 4</code></pre>"                                                                         
#> [78] "<p>All the best</p>"                                                                                         
#> [79] ""                                                                                                            
#> [80] ""                                                                                                            
#> [81] ""                                                                                                            
#> [82] "<!-- code folding -->"                                                                                       
#> [83] ""                                                                                                            
#> [84] ""                                                                                                            
#> [85] ""                                                                                                            
#> [86] "</body>"                                                                                                     
#> [87] "</html>"                                                                                                     
#> [88] "--ca343ffac9750dc5ef8b589d555af0b085a9715d1f5cf9a562e8b3d77cca4854--"