'How to send a bulk email with csv file and a jpg image attachments using gmailr
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.
Solution 1:[1]
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 the gm_mime(...) step. Does this work?
#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) %>%
gm_attach_file("iris.csv", type = "csv"))
#sending emails
safe_send_message <- safely(send_message)
sent_mail <- emails %>%
map(safe_send_message)
Solution 2:[2]
Created this function
prepare_and_send <- function(sender, recipient,
title, text,
attachment) {
email <- gm_mime() %>%
gm_to(recipient) %>%
gm_from(sender) %>%
gm_subject(title) %>%
gm_html_body(text) %>%
gm_attach_file(attachment, type = "pdf")
email <- gm_attach_part(email,msg) %>%
gm_send_message()
}
Then iterate through the email addresses in the CSV file
addresses %>%
mutate(
to = sprintf('%s <%s>', name, email),
from = email_sender,
subject = sprintf('Logistic options for %s', name),
msg = sprintf('Dear %s, %s',name,info),
attachment = sprintf('%s.pdf', attach)) %>%
mutate(x = pmap(list(from, to, subject, msg, attachment),
safely(prepare_and_send)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John-Henry |
| Solution 2 | manoj rasika |

