-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailMe.R
79 lines (61 loc) · 2.85 KB
/
mailMe.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
library(gmailr)
library(tidyverse)
library(here)
library(data.table)
library(stringr)
#First you have to set up gmailr to work from your gmail account. Then...
# Load contact list and html template
contactList <- fread(here::here("EmailList.csv"))
html_doc <- read_file(here::here("TemplateEmail.html"))
subject_line <- read_file(here::here("TemplateSubjectLine.txt"))
from_email_address <- "[email protected]"
# Adds columns to 'contactList' with the emplates, after they've been replaced for each person
addTemplateWithReplacements <- function(contactList=contactList,
html_doc=html_doc,
subject_line=subject_line){
#duplicate contact list with colnames prefixed by "replace"
contactList2<-contactList
names(contactList2)<-paste0("replace",names(contactList))
#add the html template
#replace all the mail merge fields i.e. replaceFirstName - > FirstName
contactListWithHtml<-contactList %>%
group_by(Email) %>% # do the next mutate separately per email
mutate(my_html_doc = html_doc %>% #add the html
str_replace_all(pattern = unlist(contactList2 %>% #make a named vector for str_replace_all
filter(replaceEmail == Email))),
my_subject = subject_line %>% #add the subject line
str_replace_all(pattern = unlist(contactList2 %>% #make a named vector for str_replace_all
filter(replaceEmail == Email))) #add the subject line
)%>%
ungroup()
return(contactListWithHtml)
}
# Function to check the template replacements before sending.
saveDemoEmail <- function(contactListWithHtml=contactListWithHtml,
which_one = 1){
email_content<-contactListWithHtml$my_html_doc[which_one]
fwrite(list(email_content),
file = here::here(paste0("example_content_",which_one,".html")),
quote = F)
email_subject<-contactListWithHtml$my_subject[which_one]
fwrite(list(email_subject),file = here::here(paste0("example_subject_",which_one,".txt")),
quote = F)
}
# Function to send the all the emails
sendThemAll <- function(contacts_with_html=contactListWithHtml,
from_address=from_email_address){
for ( i in 1:nrow(contacts_with_html) ) {
mime() %>%
subject(contacts_with_html[i,"my_subject"][[1]]) %>%
to(contacts_with_html[i,"Email"][[1]]) %>%
from(from_address) %>%
html_body(contacts_with_html[i,"my_html_doc"][[1]])%>%
send_message()
}
}
contactListWithHtml <- addTemplateWithReplacements(contactList=contactList,
html_doc=html_doc,
subject_line=subject_line)
saveDemoEmail(contactListWithHtml,1)
sendThemAll()
#This bit sends all the emails