DEV Community

Andrew B. Collier
Andrew B. Collier

Posted on • Originally published at datawookie.dev on

{emayili} Rendering Plain Markdown

{emayili} Rendering Plain Markdown

We’ve been able to attach text and HTML content to messages with {emayili}. But something that I’ve really been wanting to do is render markdown directly into an email.

In version 0.4.19 I’ve added the ability to directly render plain markdown into a message.

library(emayili)
Enter fullscreen mode Exit fullscreen mode

The render() method will handle plain markdown either as a character vector or from a file.

Markdown String

Let’s start with markdown in a character vector.

email <- envelope() %>%
  render("[This](https://www.google.com) is a link.")
Enter fullscreen mode Exit fullscreen mode

What’s the raw email document look like?

print(email, TRUE)

Date: Fri, 10 Sep 2021 03:42:47 GMT
X-Mailer: {emayili}-0.4.19
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="e28a25a2a39253440353e12c623"

--e28a25a2a39253440353e12c623
Content-Type: text/html; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

<p><a href=3D"https://www.google.com">This</a> is a link.</p>

--e28a25a2a39253440353e12c623--
Enter fullscreen mode Exit fullscreen mode

The markdown has been translated into HTML and inserted as a text/html MIME element.

Markdown in a File

You’re more likely to have your markdown in a file. Suppose that the file message.md contains the following:

[This](https://www.google.com) is a link.

- One
- Two
- Three

![](https://cran.r-project.org/Rlogo.svg)
Enter fullscreen mode Exit fullscreen mode

The render() function will identify its argument as a file path and render the contents of the file.

email <- envelope() %>%
  render("message.md")
Enter fullscreen mode Exit fullscreen mode

And this is what the resulting MIME message looks like:

print(email, TRUE)

Date: Fri, 10 Sep 2021 03:42:47 GMT
X-Mailer: {emayili}-0.4.19
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="8301b826a4415341f24323d192f"

--8301b826a4415341f24323d192f
Content-Type: text/html; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

<p><a href=3D"https://www.google.com">This</a> is a link.</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<p><img src=3D"https://cran.r-project.org/Rlogo.svg" alt=3D"" /></p>

--8301b826a4415341f24323d192f--
Enter fullscreen mode Exit fullscreen mode

This new feature partially scratches a persistent itch. But I’ll only be satisfied when I can render R markdown straight into an email. Stay tuned.

Top comments (0)