-
Notifications
You must be signed in to change notification settings - Fork 0
/
automatic_report_by_email.R
49 lines (33 loc) · 1.35 KB
/
automatic_report_by_email.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
##Script use to send e-mail with report, if one condition will be met.
#install.packages("RDCOMClient", repos="http://www.omegahat.net/R")
library(tidyverse)
library(RDCOMClient)
# 1. Prepare a sample report
report = iris %>% group_by(Species) %>% summarise_all(.funs = sum)
write.csv(report, "raporttest.csv")
#2. E-mail preparation
# a) in some case you have to first open your outlook app manually
OutApp<-COMCreate("Outlook.Application")
OutMail=OutApp$CreateItem(0)
#define recipient, subject and message text
OutMail[["TO"]]= "dawidtararuj@10g.pl"
OutMail[["Subject"]]="X report"
OutMail[["body"]]="report attached below"
#indicate the attachment, our report
OutMail[["Attachments"]]$Add(file.path(getwd(),"raporttest.csv"))
#3 Send an email when certain condition will be met
ifelse(report %>% select(2) %>% sum() > 499,OutMail$Send(),"email not sent")
#4 Send an email to defined recipients
recipients = c("dawidtararuj@10g.pl", "dawidtataruj@10g.pl","dawidtararuj2@10g.pl")
if(report %>% select(2) %>% sum() > 499){
for (email in recipients){
OutApp<-COMCreate("Outlook.Application")
OutMail=OutApp$CreateItem(0)
OutMail[["TO"]]= email
OutMail[["Subject"]]="X report"
OutMail[["body"]]="Report attached below"
OutMail[["Attachments"]]$Add("C:/Users/Dawid/Desktop/raporttest.csv")
OutMail$Send()}
}else{
stop()
}