Embedding hyperlinks in the text of a gmailr message


#1

Hi Everyone,

I'm using gmailr to send messages to groups of people for a research project. I've successfully followed @jennybryan 's tutorial to send pilots of my emails. Currently, the links in my emails look like this: tutorial https://github.com/jennybc/send-email-with-r. Would anyone happen to know if / how I can compose a message in R that embeds hyperlinks in the text when the email is sent (like the one that links to the tutorial)?


#2

I personally haven't played with using r to send emails in years, but I am pretty certain you can just use html_body for html markups.

Here's a vignette https://cran.r-project.org/web/packages/gmailr/vignettes/sending_messages.html

mime() %>%
  to("james.f.hester@gmail.com") %>% #> i can't believe he put his personal email here
  from("me@somewhere.com") %>%
  html_body("<b>Gmailr</b> is a <i>very</i> handy package! Check out <a href='example.com'>this link</a>.") -> html_msg

I really like the compose-and-send-your-emails customization of Jenny's tutorial. Great tip!


#3

Continuing the discussion from Embedding hyperlinks in the text of a gmailr message:

Thanks so much! I did see this vignette once before, but your pointing me back to it definitely helped me find my way to my solution. I also think I misspecified my problem in the original post. I was having trouble getting the links embedded, and that vignette helped to accomplish that.

Additionnally, I was also having trouble using map or one of its variants to change the body of my message from text to html after creating the mime message. Eventually, I figured out I could just change the attribute within the pmap function near the end of Jenny Bryan's tutorial. I put an example (with fake names, emails, website, and message) below that is illustrates how I adapted adopted Jenny Bryan’s tutorial for my context. All of the steps are the same except for the bit near the end (commented).

> addressees %>% select(first_name, last_name, email)
# A tibble: 2 x 3
  first_name last_name email              
  <chr>      <chr>     <chr>              
1 Elois      Barrows   ebarrows@gmail.com
2 Larry      Barrows   lbarrows@gmail.com

sub <- "Can I borrow some flour"
email_sender <- 'Your Neighbor <neighor@gmail.com>'

body <- "Dear  %s,
<br>
<br>
Would you mind if I stopped by after work to borrow some of that flour you bought from this  (<a href='http://www.buygreatflourhere.com/'>online vendor</a>)? 
<br>
<br>
Best, <br>
Your Neighbor
"

email_dat <- addressees %>%
  mutate(
    To = sprintf('%s %s <%s>', first_name, last_name, email),
    From = email_sender,
    Subject = sub,
    body = sprintf(body, first_name, sub)
    ) %>%
  select(To, From, Subject, body)
write_csv(edat, "data/trial-composed-emails.csv")


emails <- email_dat %>%
  pmap(., mime, 
       attr = list(content_type = "text/html")) #this line right here converted the body of the email from "text/plain" to "text/html"
str(emails, max.level = 2, list.len = 2)

safe_send_message <- safely(send_message)
sent_mail <- emails %>%
  map(safe_send_message)

saveRDS(sent_mail,
        paste('data/',gsub("\\s+", "_", sub), "_trial-sent-emails.rds", sep = ""))