We can use blastula
package from RStudio (Posit) for this. https://github.com/rstudio/blastula
I have tested this with my gmail account and it works well. The benefit of this package is that we can insert Markdown text, block-based components, and even HTML fragments. The resulting email message is responsive so it’ll look great on both large displays and mobile devices.
library(blastula)
date_time <- add_readable_time()
email <-
compose_email(
body = md(glue::glue(
"Hello,
This is to let you know that I could successfully send an email from R.
")),
footer = md(glue::glue("Email sent on {date_time}."))
)
You can run email
in the console to see how the mail would look.

Send the email -
email |>
smtp_send(
to = "toemail@ygmail.com",
from = "fromemail@gmail.com",
subject = "Test Email",
credentials = creds_file("gmail_creds")
)
Before running the above line with smtp_send
please go through the initial one time setup steps below which is important to send an email.
What is gmail_creds
?
gmail_creds
is a file where your google account information is saved.
How to create gmail_creds
file?
Now if you are using blastula
package for the first time you need to give it permission to your email. For that create an app password from https://myaccount.google.com/apppasswords . Note that, you will be allowed to create an app password only if you have turned on 2-factor authentication. Copy the app password generated from google.
Come back to R and run
create_smtp_creds_key(id = "gmail_creds",
provider = "gmail",
user = "fromemail@gmail.com")
A prompt would appear asking you to enter the password for fromemail@gmail.com
. This is where you enter the app password that you had copied in the previous step.
Now you are good to send emails from R. You can run smtp_send
function to send emails.