I want to send a bulk email with an attachment. The list of receiving email addresses and the body of the email are in a CSV file as below.
I want to attach a CSV file and a JPG image to each email. I tried the following code, but I do not understand where to put the argument to attach the above files.
suppressPackageStartupMessages(library(gmailr))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(plyr))
suppressPackageStartupMessages(library(purrr))
library(readr)
#Assigning the client contact information file.
my_dat <- read_csv("ClientContactForm.csv")
# creating a CSV file
write.csv(file = "iris.csv", iris)
#Assigning Company name, sender,BCC and body
this_hw <- "Miller Logistics"
email_sender <- 'Miller Logistics <[email protected]>'
optional_bcc <- 'Anonymous <[email protected]>'
body <- "Dear, %s %s
%s.
Thanks for desiding to transport with us!
our web site
https://www.predictea.com/
Best Regards,
Miller
"
#Creating the dataframe with above information and subject
edat <- my_dat %>%
mutate(
To = sprintf('%s <%s>', Name, Email),
Bcc = optional_bcc,
From = email_sender,
Subject = sprintf('Logistics Options for %s %s', Mr_or_Miss, Surname),
body = sprintf(body, Name, Surname, Text)) %>%
select(To, Bcc, From, Subject, body)
write_csv(edat, "data-frame.csv")
# This allow to have html links embedded in the body
emails <- edat %>%
pmap(gm_mime,attr = list(content_type = "text/html"))
str(emails, max.level = 2, list.len = 2)
#creating mime object
emails <- plyr::dlply(edat, ~ To, function(x) gm_mime(
To = x$To,
Bcc = x$Bcc,
From = x$From,
Subject = x$Subject,
body = x$body))
# trying to attach the CSV file to the email
emails <- gm_attach_file(emails,"iris.csv")
#sending emails
safe_send_message <- safely(send_message)
sent_mail <- emails %>%
map(safe_send_message)
When I execute the code, emails deliver successfully, but without the attachment. Please help. Thank you in advance.
It's hard to say based on how you wrote the question but basically you need to get the
gm_attach_file
as part of thegm_mime(...)
step. Does this work?