From 7d1ddd6258d9363b9fa5442d2c355ba991da1f02 Mon Sep 17 00:00:00 2001 From: Ignacio Date: Sat, 17 Feb 2024 14:16:15 +0100 Subject: [PATCH 01/18] enabled sending certificates via email --- DESCRIPTION | 5 +- NEWS.md | 4 + R/certificate_attendance.R | 43 ++++++++++ R/certificate_participation.R | 44 +++++++++- R/zzz.R | 30 +++++++ README.Rmd | 106 +++++++++++++++++++++--- README.md | 80 ++++++++++++++++++ inst/gmailr/labeleR_JSON.json | 1 + man/create_attendance_certificate.Rd | 7 ++ man/create_participation_certificate.Rd | 7 ++ 10 files changed, 314 insertions(+), 13 deletions(-) create mode 100644 inst/gmailr/labeleR_JSON.json diff --git a/DESCRIPTION b/DESCRIPTION index df2c07f..78b0f22 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: labeleR Title: Automate the Production of Custom Labels, Badges, Certificates, and Other Documents -Version: 0.1.3 +Version: 0.1.4 Authors@R: c( person("Ignacio", "Ramos-Gutierrez",, "ig.ramosgutierrez@gmail.com", role = c("aut", "cre", "cph"), @@ -29,4 +29,5 @@ Suggests: testthat (>= 3.0.0) Config/testthat/edition: 3 Imports: - rmarkdown + rmarkdown, + gmailr diff --git a/NEWS.md b/NEWS.md index d058940..3464941 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# labeleR 0.1.4 + +* Allow sending certificates via mail + # labeleR 0.1.3 * Allow bold and italic text within column texts diff --git a/R/certificate_attendance.R b/R/certificate_attendance.R index 9fb3b9e..f24b971 100644 --- a/R/certificate_attendance.R +++ b/R/certificate_attendance.R @@ -7,6 +7,9 @@ #' @param filename Character. Filename of the pdf. If NULL, default is "Attendance" for English, "Asistencia" for Spanish". #' @param language Character. Select 'English' or 'Spanish'. #' @param name.column Character. Name of the column in `data` storing attendees' name. +#' @param email.column Character. Name of the column in `data` storing attendees' emails to automatically send them their certificates. +#' @param email.info List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. +#' Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email. #' @param type Character (optional). Type of event (conference, workshop, seminar...) #' @param title Character. Title of the event #' @param date Date of the event @@ -58,6 +61,8 @@ create_attendance_certificate <- function( filename = NULL, language = c("English", "Spanish"), name.column = NULL, + email.column = NULL, + email.info = NULL, type = "", title = "", date = "", @@ -96,9 +101,24 @@ create_attendance_certificate <- function( if (language == "Spanish") {filename <- "Asistencia"} } + if(!is.null(email.column) & is.null(email.info)){ + stop("You must specify your email account information") + } + + if(!is.null(email.info) & + (!inherits(email.info, "list") | !("account" %in% names(email.info)) ) + ){ + stop("'email.info' should be a list including at least a slot named \"account\".\n" , + "Optionally, slots 'subject' and 'body' can also be included") + } + check_column_in_df(data, name.column) data[,name.column]<- check_latex(data, name.column) + if(!is.null(email.column)){ + check_column_in_df(data, email.column) + } + stopifnot(is.character(type)) stopifnot(is.character(title)) stopifnot(is.character(freetext)) @@ -192,6 +212,29 @@ create_attendance_certificate <- function( signer.role = if (signer.role == "") {bl.char} else {signer.role} ) ) + if(!is.null(email.column)){ + if(!is.na(data[i,email.column])){ + mail.to <- data[i,email.column] + mail.from <- email.info$account + mail.subj <- email.info$subject + mail.body <- email.info$body + + if(is.null(mail.subj)){mail.subj <- paste0("Attendance certificate - ", data[i, name.column])} + + if(is.null(mail.body)){mail.body <- paste0("Attendance certificate for ", data[i, name.column], + " awarded for attending:","\n", + title,"\n", + "\n\n\n", + "This certificate was automatically sent by labeleR using 'gmailr'")} + + send_certificate_mail( + from = mail.from, + to = mail.to, + subject = mail.subj, + body = mail.body, + attach = paste0(path, "/", output_file) + ) + }} } diff --git a/R/certificate_participation.R b/R/certificate_participation.R index 875a1ce..1e0dee3 100644 --- a/R/certificate_participation.R +++ b/R/certificate_participation.R @@ -15,6 +15,9 @@ #' contribution. #' @param date.column Character. Name of the column in `data` storing dates of #' participation. +#' @param email.column Character. Name of the column in `data` storing attendees' emails to automatically send them their certificates. +#' @param email.info List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. +#' Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email. #' @param type Character (optional). Type of event (conference, workshop, seminar...) #' @param event Character. Title of the event #' @param freetext Character (optional). Free text to insert before the date. @@ -69,6 +72,8 @@ create_participation_certificate <- function( comm.type.column = NULL, title.column = NULL, date.column = NULL, + email.column = NULL, + email.info = NULL, type = "", event = "", freetext = "", @@ -105,6 +110,17 @@ create_participation_certificate <- function( if (language == "Spanish") {filename <- "Participacion"} } + if(!is.null(email.column) & is.null(email.info)){ + stop("You must specify your email account information") + } + + if(!is.null(email.info) & + (!inherits(email.info, "list") | !("account" %in% names(email.info)) ) + ){ + stop("'email.info' should be a list including at least a slot named \"account\".\n" , + "Optionally, slots 'subject' and 'body' can also be included") + } + check_column_in_df(data, name.column) if (!is.null(affiliation.column)) { @@ -118,6 +134,10 @@ create_participation_certificate <- function( check_column_in_df(data, date.column) + if(!is.null(email.column)){ + check_column_in_df(data, email.column) + } + arguments <- c(name.column, comm.type.column, title.column, date.column) arguments <- arguments[arguments!=""] @@ -210,7 +230,29 @@ create_participation_certificate <- function( signer.role.i = if (signer.role == "") {bl.char} else {signer.role} ) ) - + if(!is.null(email.column)){ + if(!is.na(data[i,email.column])){ + mail.to <- data[i,email.column] + mail.from <- email.info$account + mail.subj <- email.info$subject + mail.body <- email.info$body + + if(is.null(mail.subj)){mail.subj <- paste0("Attendance certificate - ", data[i, name.column])} + + if(is.null(mail.body)){mail.body <- paste0("Attendance certificate for ", data[i, name.column], + " awarded for attending:","\n", + data[i, title.column],"\n", + "\n\n\n", + "This certificate was automatically sent by labeleR using 'gmailr'")} + + send_certificate_mail( + from = mail.from, + to = mail.to, + subject = mail.subj, + body = mail.body, + attach = paste0(path, "/", output_file) + ) + }} } } diff --git a/R/zzz.R b/R/zzz.R index 4c15222..9f0dd79 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -95,3 +95,33 @@ use_image <- function(image = NULL, name = NULL, folder = NULL) { } } + + +#### Function to end gmail certificate + +send_certificate_mail <- function(from = NULL, to = NULL, + subject = "email automatically sent by labeleR", + body = "", + attach = NULL){ + + if(is.null(from)){stop("A valid email account must be specified. Please make sure to access the Google API with the same account.")} + if(is.null(to)){stop("A destination email account must be specified")} + if(is.null(body)){body <- "This certificate was automatically sent by labeleR using 'gmailr'"} + if(is.null(attach)){stop("A pdf document must be attached.")} + + gmailr::gm_auth_configure(path = system.file("gmailr/labeleR_JSON.json", package = "labeleR")) + + + + email <- + gmailr::gm_mime() |> + gmailr::gm_to(to) |> + gmailr::gm_from(from) |> + gmailr::gm_subject(subject) |> + gmailr::gm_text_body(body) |> + gmailr::gm_attach_file(attach) + + + gmailr::gm_send_message(email) + +} diff --git a/README.Rmd b/README.Rmd index e7b2d19..1e79a6c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -19,6 +19,7 @@ knitr::opts_chunk$set( # labeleR + `r badger::badge_cran_release()` ![](https://img.shields.io/github/r-package/v/EcologyR/labeleR) [![R-CMD-check](https://github.com/EcologyR/labeleR/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/EcologyR/labeleR/actions/workflows/R-CMD-check.yaml) @@ -27,6 +28,7 @@ knitr::opts_chunk$set( [![HitCount](https://hits.dwyl.com/EcologyR/labeleR.svg?style=flat-square)](https://hits.dwyl.com/EcologyR/labeleR) [![HitCount](https://hits.dwyl.com/EcologyR/labeleR.svg?style=flat-square&show=unique)](https://hits.dwyl.com/EcologyR/labeleR) [![](https://cranlogs.r-pkg.org/badges/grand-total/labeleR)](https://cran.r-project.org/package=labeleR) + @@ -76,15 +78,18 @@ The first time you install tinytex or use **labeleR** it may take a while until all packages are correctly installed. Don't worry, it will be much quicker next time! - #### TinyTeX troubleshooting -In case you have problems installing `TinyTeX`'s packages, try running this in your console: + +In case you have problems installing `TinyTeX`'s packages, try running +this in your console: + ```{r TinyTeX pkgs, eval = FALSE} tinytex::tlmgr_install(pkgs = c( "zref", "needspace", "pagecolor", "bookmark", "changepage", "fp", "mdframed", "ms", "pgf", "pspicture", "qrcode")) ``` + ## 1. Getting started ```{r load labeleR, eval = FALSE} @@ -263,6 +268,8 @@ document. | | +-----------------------------------------------------------------------+ +#### **NOTE:** To see how to send certificates automatically *via* email, go to the FAQ section! + ### 2.3 Badges Badges (and all documents from now onward) are rendered in a single @@ -443,16 +450,16 @@ create_tiny_label( ### Including italic or bold texts within a single text This implementation should be used just to change text formats in only a -part of values stored in columns (variable parameters). +part of values stored in columns (variable parameters). -To do so, you must edit the cell value, specifying where the italics text must start -with `\\textit`, and `\\end` where it ends; and `\\textbf` followed by -`\\end` for bold text. In case you want to combine both, you will have -to specify `\\end` twice. +To do so, you must edit the cell value, specifying where the italics +text must start with `\\textit`, and `\\end` where it ends; and +`\\textbf` followed by `\\end` for bold text. In case you want to +combine both, you will have to specify `\\end` twice. -For example, this could be helpful to -include italics in a species name which is included as part of a title; -where just the species name should be italicized. +For example, this could be helpful to include italics in a species name +which is included as part of a title; where just the species name should +be italicized. ```{r custom italics, eval=FALSE} seminar.table <- data.frame( @@ -492,6 +499,85 @@ create_participation_certificate( | | +-----------------------------------------------------------------------+ +### Sending certificates automatically using `gmailr` + +Both attendance and participation certificates can be sent automatically +using a gmail account. All you have to do is to specify a mail from +which you want to send them (it will open a connection through an +authentication step), and specify a column name where the email +recipient's accounts are stored. + +**NOTE**: The account to be used will be asked only once in each +session. To change the sending account, you must restart R session. Once +you have specified your email account, you must enable labeleR +application to have access to your mail. + +To do so, you must first ensure you have installed the `gmailr` package: + +```{r load gmailr, eval = FALSE} +require("gmailr") +``` + +To send emails automatically you have to specify, at least, a google +account within a list. This object must contain a slot named "account", +where the email must be specified. Additionally, you can also specify +the email subject in a slot named "subject", and the body content of the +email in a slot named "body". These latter two are optional, and if not +specified, default values will be applied. + +```{r email list, include = FALSE} +students.table <- data.frame( + "Names" = c("Harry Potter", "Hermione Granger", "Neville Longbottom", "Ginny Weasley"), + "email" = c("h.potter@hogwarts.co.uk", "h.granger@hogwarts.co.uk", + "n.longbottom@hogwarts.co.uk", "g.weasley@hogwarts.co.uk")) + +``` + +Now we have a list of attendees with their emails + +```{r see table} +print(students.table) +``` + +If we specify the email column and our email information, we can send +everything automatically! + +```{r send email, eval=FALSE} + +emailinfo <- list( + "account" = "a.dumbledore@hogwarts.co.uk" + #, "subject" = "Class certificates - School Year 1992-1993" #optional to use non-default values + #, "body" = "Here you have your certificate, dear student!" #optional to use non-default values +) + + + +create_attendance_certificate( + data = students.table, + path = "labeleR_output", + filename = "attendance_certificates", + language = "English" , + name.column = "Names", + + email.column = "email", + email.info = emailinfo, + + type = "class", + title = "Potions (year 1992-1993)", + date = "23/06/1993", + hours = "200", + freetext = "taught by Professor S. Snape", + signer = "A.P.W.B. Dumbledore", + signer.role = "School Headmaster", + rpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), + lpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), + signature.pic = system.file("rmarkdown/pictures/dumbledore.png", package = "labeleR") +) + +``` + +This way, every student will receive an email with their individual certificate attached. + ## Citation ```{r citation, eval=TRUE} diff --git a/README.md b/README.md index b227c83..5820566 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.re [![HitCount](https://hits.dwyl.com/EcologyR/labeleR.svg?style=flat-square)](https://hits.dwyl.com/EcologyR/labeleR) [![HitCount](https://hits.dwyl.com/EcologyR/labeleR.svg?style=flat-square&show=unique)](https://hits.dwyl.com/EcologyR/labeleR) [![](https://cranlogs.r-pkg.org/badges/grand-total/labeleR)](https://cran.r-project.org/package=labeleR) + @@ -247,6 +248,8 @@ document. |--------------------------------------------------------------------------| | | +#### **NOTE:** To see how to send certificates automatically *via* email, go to the FAQ section! + ### 2.3 Badges Badges (and all documents from now onward) are rendered in a single @@ -471,6 +474,83 @@ create_participation_certificate( |------------------------------------------------------------------------------| | | +### Sending certificates automatically using `gmailr` + +Both attendance and participation certificates can be sent automatically +using a gmail account. All you have to do is to specify a mail from +which you want to send them (it will open a connection through an +authentication step), and specify a column name where the email +recipient’s accounts are stored. + +**NOTE**: The account to be used will be asked only once in each +session. To change the sending account, you must restart R session. Once +you have specified your email account, you must enable labeleR +application to have access to your mail. + +To do so, you must first ensure you have installed the `gmailr` package: + +``` r +require("gmailr") +``` + +To send emails automatically you have to specify, at least, a google +account within a list. This object must contain a slot named “account”, +where the email must be specified. Additionally, you can also specify +the email subject in a slot named “subject”, and the body content of the +email in a slot named “body”. These latter two are optional, and if not +specified, default values will be applied. + +Now we have a list of attendees with their emails + +``` r +print(students.table) +#> Names email +#> 1 Harry Potter h.potter@hogwarts.co.uk +#> 2 Hermione Granger h.granger@hogwarts.co.uk +#> 3 Neville Longbottom n.longbottom@hogwarts.co.uk +#> 4 Ginny Weasley g.weasley@hogwarts.co.uk +``` + +If we specify the email column and our email information, we can send +everything automatically! + +``` r + +emailinfo <- list( + "account" = "a.dumbledore@hogwarts.co.uk" + #, "subject" = "Class certificates - School Year 1992-1993" #optional to use non-default values + #, "body" = "Here you have your certificate, dear student!" #optional to use non-default values +) + + + +create_attendance_certificate( + data = students.table, + path = "labeleR_output", + filename = "attendance_certificates", + language = "English" , + name.column = "Names", + + email.column = "email", + email.info = emailinfo, + + type = "class", + title = "Potions (year 1992-1993)", + date = "23/06/1993", + hours = "200", + freetext = "taught by Professor S. Snape", + signer = "A.P.W.B. Dumbledore", + signer.role = "School Headmaster", + rpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), + lpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), + signature.pic = system.file("rmarkdown/pictures/dumbledore.png", package = "labeleR") +) + +``` + +This way, every student will receive an email with their individual +certificate attached. + ## Citation ``` r diff --git a/inst/gmailr/labeleR_JSON.json b/inst/gmailr/labeleR_JSON.json new file mode 100644 index 0000000..bdd2396 --- /dev/null +++ b/inst/gmailr/labeleR_JSON.json @@ -0,0 +1 @@ +{"installed":{"client_id":"304947191875-is6hh61qcun65jb95ikvpqt3pm5o318v.apps.googleusercontent.com","project_id":"labeler-414610","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}} \ No newline at end of file diff --git a/man/create_attendance_certificate.Rd b/man/create_attendance_certificate.Rd index bd8bf5a..71f8eac 100644 --- a/man/create_attendance_certificate.Rd +++ b/man/create_attendance_certificate.Rd @@ -10,6 +10,8 @@ create_attendance_certificate( filename = NULL, language = c("English", "Spanish"), name.column = NULL, + email.column = NULL, + email.info = NULL, type = "", title = "", date = "", @@ -35,6 +37,11 @@ create_attendance_certificate( \item{name.column}{Character. Name of the column in \code{data} storing attendees' name.} +\item{email.column}{Character. Name of the column in \code{data} storing attendees' emails to automatically send them their certificates.} + +\item{email.info}{List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. +Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email.} + \item{type}{Character (optional). Type of event (conference, workshop, seminar...)} \item{title}{Character. Title of the event} diff --git a/man/create_participation_certificate.Rd b/man/create_participation_certificate.Rd index dedeee9..759d4ee 100644 --- a/man/create_participation_certificate.Rd +++ b/man/create_participation_certificate.Rd @@ -14,6 +14,8 @@ create_participation_certificate( comm.type.column = NULL, title.column = NULL, date.column = NULL, + email.column = NULL, + email.info = NULL, type = "", event = "", freetext = "", @@ -49,6 +51,11 @@ contribution.} \item{date.column}{Character. Name of the column in \code{data} storing dates of participation.} +\item{email.column}{Character. Name of the column in \code{data} storing attendees' emails to automatically send them their certificates.} + +\item{email.info}{List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. +Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email.} + \item{type}{Character (optional). Type of event (conference, workshop, seminar...)} \item{event}{Character. Title of the event} From bbcf637ac3e8b062a9adfa1e6ba11463d5d5522e Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Mon, 7 Oct 2024 14:34:50 +0200 Subject: [PATCH 02/18] primary function to send emails using secret token --- R/sendmails_(temp).R | 43 ++++++++++++++++++++++++++++++++++ inst/gmailr/gmailr-token.rds | Bin 0 -> 6671 bytes inst/gmailr/labeleR_JSON.json | 1 - 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 R/sendmails_(temp).R create mode 100644 inst/gmailr/gmailr-token.rds delete mode 100644 inst/gmailr/labeleR_JSON.json diff --git a/R/sendmails_(temp).R b/R/sendmails_(temp).R new file mode 100644 index 0000000..d63cd64 --- /dev/null +++ b/R/sendmails_(temp).R @@ -0,0 +1,43 @@ +#sendmails +library(gmailr) + + +tk <- gmailr::gm_token_read( + path = "inst/gmailr/gmailr-token.rds" +) +gmailr::gm_auth_configure(tk$client) + +gm_auth( + email = gm_default_email(), + path = , + subject = NULL, + scopes = "full", + cache = gargle::gargle_oauth_cache(), + use_oob = gargle::gargle_oob_default(), + token = NULL +) + + + +from <- "Needed? TEMPLATE MAIL SENDER" +to <- "mail@place.com" +subject <- "labeleR certificate - NEW" +body <- paste0("Certificate of participation", + "\n\n\n", + "This certificate was automatically sent by labeleR using 'gmailr'") +attach <- system.file("rmarkdown/pictures/Hogwarts_BnW.png", package = "labeleR") + + + + + +email <- + gmailr::gm_mime() |> + gmailr::gm_to(to) |> + gmailr::gm_from(from) |> + gmailr::gm_subject(subject) |> + gmailr::gm_text_body(body) |> + gmailr::gm_attach_file(attach) + + +gmailr::gm_send_message(email) diff --git a/inst/gmailr/gmailr-token.rds b/inst/gmailr/gmailr-token.rds new file mode 100644 index 0000000000000000000000000000000000000000..95c5c73a0181945860c4d35903849b44b02d9f58 GIT binary patch literal 6671 zcmV+q8t~;dDq25m{FTWJBxXA=E28Ml>5ja_rcAwG&|ANziZ_QopM`~T1N-}-1bOWO z>@LPL?zY&X6DEeCxw39c<1v@j)@pvITNqtLYHB9aB=n`MWEFU&6t z>KH|3w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%m8nOIR6waAoS0s5R;3mt{$%H!p@L*jtCfv-|KXE4Z61vp~eF_VZbc(^*vLIr8%b*6e*rO1mT1*v(D#)=Xzt3Z15VKnq0tKNXO$-F*7<_OQnXQNc~fK$3-2uRlp^(SSKSDN)mF07sQj?=foAm?Y(c|)+LD>gNW=`(=lz!X;ptegg5YZ# zQAES7o%+XRdI1FZ=~7kKg307D%fkowKF+!KeDXuLFk>D5wHE6EC0PoN_e;L6B%zQY z!<8EMo67+g7c}Oq=(V;QPxty-I|^t*Z|Ois=t72El4as&7GcCW9Inoc0AID|%_Ijt z@A#RhgRnrNVJ|Vaa#_s84o0#+})5e5w^4{3b8c&F;<*teusJ>uoPPzKsqEKd;DA@v|NG4;u>O3rV z|5V`Za83%n5Oe|^P1c&gq7d23scIkx*=p>${Xv~R>>k%&2OEUQ1}4t)V=i4v>Z1fL zD|^3wsdmTNNF;BL9q(fKiN7_FDBm_4=G4*fa6(V+h=eW65B4q1mJxG7XZ6KNkf?%p zJOu&mBr#-KUBZSUBaJF0u^WU#p3G)XJA23&-N-|doMx_y?ejzSdj~>@<291G6kjMl ZsoN;{h;#0i%N2nRQGuT`vJ5&2mRw87G(G?T literal 0 HcmV?d00001 diff --git a/inst/gmailr/labeleR_JSON.json b/inst/gmailr/labeleR_JSON.json deleted file mode 100644 index bdd2396..0000000 --- a/inst/gmailr/labeleR_JSON.json +++ /dev/null @@ -1 +0,0 @@ -{"installed":{"client_id":"304947191875-is6hh61qcun65jb95ikvpqt3pm5o318v.apps.googleusercontent.com","project_id":"labeler-414610","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}} \ No newline at end of file From ba18fdabd3dec8ead934138ec4045c8316156917 Mon Sep 17 00:00:00 2001 From: Ignacio Date: Mon, 28 Oct 2024 13:31:33 +0100 Subject: [PATCH 03/18] sending emails automatically using blastula [skip ci] --- DESCRIPTION | 2 +- R/certificate_attendance.R | 62 ++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 78b0f22..f70af99 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,4 +30,4 @@ Suggests: Config/testthat/edition: 3 Imports: rmarkdown, - gmailr + blastula diff --git a/R/certificate_attendance.R b/R/certificate_attendance.R index f24b971..010637c 100644 --- a/R/certificate_attendance.R +++ b/R/certificate_attendance.R @@ -101,17 +101,45 @@ create_attendance_certificate <- function( if (language == "Spanish") {filename <- "Asistencia"} } + if(!is.null(email.column) & !is.null(email.info)){ + sendmail <- TRUE + }else { + sendmail <- FALSE + } + + if(sendmail){ + create_smtp_creds_key(id = email.info$creds.name, + provider = "gmail", + user = email.info$account, + overwrite = T) + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == email.info$account & + credentials$id == email.info$creds.name,] + if(nrow(credentials) == 0){ + stop("You must create sour mail sending application (dont't worry, it is very easy, and is necessary only the first time!). + +- First access this link using the specified mail account: +https://myaccount.google.com/apppasswords + +- Create an application. The used application name must be specified in email.info, within the creds.name slot. + +- Save the password anywhere safe, as you will be asked for it later.") + } + + } + if(!is.null(email.column) & is.null(email.info)){ stop("You must specify your email account information") } if(!is.null(email.info) & - (!inherits(email.info, "list") | !("account" %in% names(email.info)) ) + (!inherits(email.info, "list") | !(("account") %in% names(email.info)) | !(("creds.name") %in% names(email.info))) ){ - stop("'email.info' should be a list including at least a slot named \"account\".\n" , + stop("'email.info' should be a list including at least two slots named \"account\" and \"creds.name\".\n" , "Optionally, slots 'subject' and 'body' can also be included") } + check_column_in_df(data, name.column) data[,name.column]<- check_latex(data, name.column) @@ -212,8 +240,9 @@ create_attendance_certificate <- function( signer.role = if (signer.role == "") {bl.char} else {signer.role} ) ) - if(!is.null(email.column)){ - if(!is.na(data[i,email.column])){ + + if(sendmail & !is.na(data[i,email.column])){ + mail.to <- data[i,email.column] mail.from <- email.info$account mail.subj <- email.info$subject @@ -225,16 +254,25 @@ create_attendance_certificate <- function( " awarded for attending:","\n", title,"\n", "\n\n\n", - "This certificate was automatically sent by labeleR using 'gmailr'")} + "This certificate was automatically sent by labeleR using 'blastula'")} - send_certificate_mail( - from = mail.from, - to = mail.to, - subject = mail.subj, + + email <- blastula::compose_email( body = mail.body, - attach = paste0(path, "/", output_file) - ) - }} + footer = "Mail sent on automatically using labeleR. + https://ecologyr.github.io/labeleR/") + email <- blastula::add_attachment(email, file = paste0(path, "/", output_file)) + + + blastula::smtp_send( + email, + credentials = creds_key(email.info$creds.name), + to = mail.to, + from = mail.from, + subject = mail.subj) + + + } } From c2ef0757b735690bf7fbb0ae92447619673b1424 Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Tue, 29 Oct 2024 18:25:53 +0100 Subject: [PATCH 04/18] remove gmailr script --- R/sendmails_(temp).R | 43 ------------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 R/sendmails_(temp).R diff --git a/R/sendmails_(temp).R b/R/sendmails_(temp).R deleted file mode 100644 index d63cd64..0000000 --- a/R/sendmails_(temp).R +++ /dev/null @@ -1,43 +0,0 @@ -#sendmails -library(gmailr) - - -tk <- gmailr::gm_token_read( - path = "inst/gmailr/gmailr-token.rds" -) -gmailr::gm_auth_configure(tk$client) - -gm_auth( - email = gm_default_email(), - path = , - subject = NULL, - scopes = "full", - cache = gargle::gargle_oauth_cache(), - use_oob = gargle::gargle_oob_default(), - token = NULL -) - - - -from <- "Needed? TEMPLATE MAIL SENDER" -to <- "mail@place.com" -subject <- "labeleR certificate - NEW" -body <- paste0("Certificate of participation", - "\n\n\n", - "This certificate was automatically sent by labeleR using 'gmailr'") -attach <- system.file("rmarkdown/pictures/Hogwarts_BnW.png", package = "labeleR") - - - - - -email <- - gmailr::gm_mime() |> - gmailr::gm_to(to) |> - gmailr::gm_from(from) |> - gmailr::gm_subject(subject) |> - gmailr::gm_text_body(body) |> - gmailr::gm_attach_file(attach) - - -gmailr::gm_send_message(email) From 3cac2ddc4ca291cdf2141f957b708ef21d962da7 Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Tue, 29 Oct 2024 18:28:38 +0100 Subject: [PATCH 05/18] account to user, blastula:: and md --- R/certificate_attendance.R | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/R/certificate_attendance.R b/R/certificate_attendance.R index 010637c..f82c70d 100644 --- a/R/certificate_attendance.R +++ b/R/certificate_attendance.R @@ -8,7 +8,7 @@ #' @param language Character. Select 'English' or 'Spanish'. #' @param name.column Character. Name of the column in `data` storing attendees' name. #' @param email.column Character. Name of the column in `data` storing attendees' emails to automatically send them their certificates. -#' @param email.info List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. +#' @param email.info List. Must include at least one slot named 'user' specifing a Google Mail accout to send certificates from. #' Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email. #' @param type Character (optional). Type of event (conference, workshop, seminar...) #' @param title Character. Title of the event @@ -110,15 +110,15 @@ create_attendance_certificate <- function( if(sendmail){ create_smtp_creds_key(id = email.info$creds.name, provider = "gmail", - user = email.info$account, + user = email.info$user, overwrite = T) credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == email.info$account & + credentials <- credentials[credentials$username == email.info$user & credentials$id == email.info$creds.name,] if(nrow(credentials) == 0){ stop("You must create sour mail sending application (dont't worry, it is very easy, and is necessary only the first time!). -- First access this link using the specified mail account: +- First access this link using the specified mail user: https://myaccount.google.com/apppasswords - Create an application. The used application name must be specified in email.info, within the creds.name slot. @@ -129,13 +129,13 @@ https://myaccount.google.com/apppasswords } if(!is.null(email.column) & is.null(email.info)){ - stop("You must specify your email account information") + stop("You must specify your email user information") } if(!is.null(email.info) & - (!inherits(email.info, "list") | !(("account") %in% names(email.info)) | !(("creds.name") %in% names(email.info))) + (!inherits(email.info, "list") | !(("user") %in% names(email.info)) | !(("creds.name") %in% names(email.info))) ){ - stop("'email.info' should be a list including at least two slots named \"account\" and \"creds.name\".\n" , + stop("'email.info' should be a list including at least two slots named \"user\" and \"creds.name\".\n" , "Optionally, slots 'subject' and 'body' can also be included") } @@ -241,10 +241,11 @@ https://myaccount.google.com/apppasswords ) ) - if(sendmail & !is.na(data[i,email.column])){ + if(sendmail){ + if(!is.na(data[i,email.column])){ mail.to <- data[i,email.column] - mail.from <- email.info$account + mail.from <- email.info$user mail.subj <- email.info$subject mail.body <- email.info$body @@ -258,21 +259,22 @@ https://myaccount.google.com/apppasswords email <- blastula::compose_email( - body = mail.body, - footer = "Mail sent on automatically using labeleR. - https://ecologyr.github.io/labeleR/") + body = blastula::md(mail.body), + footer = blastula::md( +"Mail sent on automatically using labeleR.\r\n +https://ecologyr.github.io/labeleR/")) email <- blastula::add_attachment(email, file = paste0(path, "/", output_file)) blastula::smtp_send( email, - credentials = creds_key(email.info$creds.name), + credentials = blastula::creds_key(email.info$creds.name), to = mail.to, from = mail.from, subject = mail.subj) - } + }} } From 9bf27adf46a8767e1ff81dfc590bf0d89fecafce Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Wed, 30 Oct 2024 12:34:10 +0100 Subject: [PATCH 06/18] sendmail function created with blastula --- R/zzz.R | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/R/zzz.R b/R/zzz.R index 9f0dd79..23339b5 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -99,29 +99,42 @@ use_image <- function(image = NULL, name = NULL, folder = NULL) { #### Function to end gmail certificate -send_certificate_mail <- function(from = NULL, to = NULL, - subject = "email automatically sent by labeleR", - body = "", - attach = NULL){ +sendmail <- function(data, row, email.info, + name.column, email.column, + attachment){ + mail.to <- data[row,email.column] - if(is.null(from)){stop("A valid email account must be specified. Please make sure to access the Google API with the same account.")} - if(is.null(to)){stop("A destination email account must be specified")} - if(is.null(body)){body <- "This certificate was automatically sent by labeleR using 'gmailr'"} - if(is.null(attach)){stop("A pdf document must be attached.")} + if(is.na(mail.to)){message("email not sent to ", data[row, name.column])}#se puede mandar un auto mensaje?? + if(!is.na(mail.to)){ - gmailr::gm_auth_configure(path = system.file("gmailr/labeleR_JSON.json", package = "labeleR")) + mail.from <- email.info$user + mail.subj <- email.info$subject + mail.body <- email.info$body + mail.cc <- email.info$cc + mail.bcc <- email.info$bcc + if(is.null(mail.subj)){mail.subj <- paste0("Certificate - ", data[row, name.column])} + if(is.null(mail.body)){mail.body <- paste0("Certificate for ", data[row, name.column],".\n\n", + "This certificate was automatically sent by labeleR using 'blastula'")} - email <- - gmailr::gm_mime() |> - gmailr::gm_to(to) |> - gmailr::gm_from(from) |> - gmailr::gm_subject(subject) |> - gmailr::gm_text_body(body) |> - gmailr::gm_attach_file(attach) + email <- blastula::compose_email( + body = blastula::md(mail.body), + footer = blastula::md( + "Mail sent on automatically using labeleR.\r\n +https://ecologyr.github.io/labeleR/")) + email <- blastula::add_attachment(email, file = attachment) - gmailr::gm_send_message(email) -} + blastula::smtp_send( + email, + credentials = blastula::creds_key(email.info$creds.name), + to = mail.to, + from = mail.from, + subject = mail.subj, + cc = mail.cc, + bcc = mail.bcc) + + + }} From 39efc67ef31c7012cb206fa31ef704611ca2236c Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Wed, 30 Oct 2024 13:43:05 +0100 Subject: [PATCH 07/18] send_mail in function and setup --- R/certificate_attendance.R | 80 +++++------------------------------ R/certificate_participation.R | 46 ++++++-------------- R/zzz.R | 71 +++++++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 106 deletions(-) diff --git a/R/certificate_attendance.R b/R/certificate_attendance.R index f82c70d..a1e72b4 100644 --- a/R/certificate_attendance.R +++ b/R/certificate_attendance.R @@ -101,43 +101,8 @@ create_attendance_certificate <- function( if (language == "Spanish") {filename <- "Asistencia"} } - if(!is.null(email.column) & !is.null(email.info)){ - sendmail <- TRUE - }else { - sendmail <- FALSE - } - - if(sendmail){ - create_smtp_creds_key(id = email.info$creds.name, - provider = "gmail", - user = email.info$user, - overwrite = T) - credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == email.info$user & - credentials$id == email.info$creds.name,] - if(nrow(credentials) == 0){ - stop("You must create sour mail sending application (dont't worry, it is very easy, and is necessary only the first time!). - -- First access this link using the specified mail user: -https://myaccount.google.com/apppasswords - -- Create an application. The used application name must be specified in email.info, within the creds.name slot. - -- Save the password anywhere safe, as you will be asked for it later.") - } - } - - if(!is.null(email.column) & is.null(email.info)){ - stop("You must specify your email user information") - } - - if(!is.null(email.info) & - (!inherits(email.info, "list") | !(("user") %in% names(email.info)) | !(("creds.name") %in% names(email.info))) - ){ - stop("'email.info' should be a list including at least two slots named \"user\" and \"creds.name\".\n" , - "Optionally, slots 'subject' and 'body' can also be included") - } + sendmail <- sendmail_setup(email.column, email.info) check_column_in_df(data, name.column) @@ -241,40 +206,15 @@ https://myaccount.google.com/apppasswords ) ) - if(sendmail){ - if(!is.na(data[i,email.column])){ - - mail.to <- data[i,email.column] - mail.from <- email.info$user - mail.subj <- email.info$subject - mail.body <- email.info$body - - if(is.null(mail.subj)){mail.subj <- paste0("Attendance certificate - ", data[i, name.column])} - - if(is.null(mail.body)){mail.body <- paste0("Attendance certificate for ", data[i, name.column], - " awarded for attending:","\n", - title,"\n", - "\n\n\n", - "This certificate was automatically sent by labeleR using 'blastula'")} - - - email <- blastula::compose_email( - body = blastula::md(mail.body), - footer = blastula::md( -"Mail sent on automatically using labeleR.\r\n -https://ecologyr.github.io/labeleR/")) - email <- blastula::add_attachment(email, file = paste0(path, "/", output_file)) - - - blastula::smtp_send( - email, - credentials = blastula::creds_key(email.info$creds.name), - to = mail.to, - from = mail.from, - subject = mail.subj) - - - }} + if(isTRUE(sendmail)){ + send_mail(data = data, + row = i, + email.info = email.info, + name.column = name.column , + email.column = email.column , + attachment = paste0(path, "/", output_file) + ) + } } diff --git a/R/certificate_participation.R b/R/certificate_participation.R index 1e0dee3..1f3cbe9 100644 --- a/R/certificate_participation.R +++ b/R/certificate_participation.R @@ -110,16 +110,9 @@ create_participation_certificate <- function( if (language == "Spanish") {filename <- "Participacion"} } - if(!is.null(email.column) & is.null(email.info)){ - stop("You must specify your email account information") - } - if(!is.null(email.info) & - (!inherits(email.info, "list") | !("account" %in% names(email.info)) ) - ){ - stop("'email.info' should be a list including at least a slot named \"account\".\n" , - "Optionally, slots 'subject' and 'body' can also be included") - } + sendmail <- sendmail_setup(email.column, email.info) + check_column_in_df(data, name.column) @@ -230,30 +223,17 @@ create_participation_certificate <- function( signer.role.i = if (signer.role == "") {bl.char} else {signer.role} ) ) - if(!is.null(email.column)){ - if(!is.na(data[i,email.column])){ - mail.to <- data[i,email.column] - mail.from <- email.info$account - mail.subj <- email.info$subject - mail.body <- email.info$body - - if(is.null(mail.subj)){mail.subj <- paste0("Attendance certificate - ", data[i, name.column])} - - if(is.null(mail.body)){mail.body <- paste0("Attendance certificate for ", data[i, name.column], - " awarded for attending:","\n", - data[i, title.column],"\n", - "\n\n\n", - "This certificate was automatically sent by labeleR using 'gmailr'")} - - send_certificate_mail( - from = mail.from, - to = mail.to, - subject = mail.subj, - body = mail.body, - attach = paste0(path, "/", output_file) - ) - }} + + if(isTRUE(sendmail)){ + send_mail(data = data, + row = i, + email.info = email.info, + name.column = name.column , + email.column = email.column, + attachment = paste0(path, "/", output_file) ) + } + } } -} + diff --git a/R/zzz.R b/R/zzz.R index 23339b5..16c3469 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -97,9 +97,71 @@ use_image <- function(image = NULL, name = NULL, folder = NULL) { } -#### Function to end gmail certificate +#### Function to set the SMTP server +sendmail_setup <- function(email.column, email.info){ -sendmail <- function(data, row, email.info, + if( is.null(email.column) & !is.null(email.info)){ + stop("You must specify the email column") + } + + if(!is.null(email.column) & is.null(email.info)){ + stop("You must specify your email information") + } + + if(!is.null(email.info) & + (!inherits(email.info, "list") | + !(("user") %in% names(email.info)) | + !(("app.name") %in% names(email.info)))){ + stop("'email.info' should be a list including at least two slots named 'user' and 'app.name'.\n" , + "Optionally, slots 'cc', 'bcc', 'subject' and 'body' can also be included") + } + + if(!is.null(email.column) & !is.null(email.info)){ + sendmail <- TRUE + }else { + sendmail <- FALSE + } + + if (isTRUE(sendmail)) { + sendmail <- utils::askYesNo("You are trying to send automatically certificates via email. Are you sure you want to continue?", + default = FALSE, + prompts = getOption("askYesNo", gettext(c("Yes", "No", "Cancel")))) + } + + if(isTRUE(sendmail)){ + + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == email.info$user & + credentials$id == email.info$app.name,] + if(nrow(credentials) == 0){ + message("You must create sour mail sending application (dont't worry, it is very easy, and is necessary only the first time!). + +- First access this link using the specified mail user (R will open it for you): +https://myaccount.google.com/apppasswords + +- Create an application. The used application name must be specified in email.info, within the app.name slot. + +- Save the password anywhere safe, as you will be asked for it later!\n\n") + + utils::browseURL("https://myaccount.google.com/apppasswords") + } + + blastula::create_smtp_creds_key(id = email.info$app.name, + provider = "gmail", + user = email.info$user, + overwrite = T) + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == email.info$user & + credentials$id == email.info$app.name,] + + } + + return(sendmail) +} + +#### Function to send a mail and atachment within the loop + +send_mail <- function(data, row, email.info, name.column, email.column, attachment){ mail.to <- data[row,email.column] @@ -129,7 +191,7 @@ https://ecologyr.github.io/labeleR/")) blastula::smtp_send( email, - credentials = blastula::creds_key(email.info$creds.name), + credentials = blastula::creds_key(email.info$app.name), to = mail.to, from = mail.from, subject = mail.subj, @@ -138,3 +200,6 @@ https://ecologyr.github.io/labeleR/")) }} + + + From 99a0db10c7020e178793c4b5f1c2733950e2a762 Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Mon, 11 Nov 2024 12:52:34 +0100 Subject: [PATCH 08/18] erase all gmailr info & documentation --- README.Rmd | 77 ----------------------------------- inst/gmailr/gmailr-token.rds | Bin 6671 -> 0 bytes 2 files changed, 77 deletions(-) delete mode 100644 inst/gmailr/gmailr-token.rds diff --git a/README.Rmd b/README.Rmd index 1e79a6c..7131b59 100644 --- a/README.Rmd +++ b/README.Rmd @@ -499,84 +499,7 @@ create_participation_certificate( | | +-----------------------------------------------------------------------+ -### Sending certificates automatically using `gmailr` -Both attendance and participation certificates can be sent automatically -using a gmail account. All you have to do is to specify a mail from -which you want to send them (it will open a connection through an -authentication step), and specify a column name where the email -recipient's accounts are stored. - -**NOTE**: The account to be used will be asked only once in each -session. To change the sending account, you must restart R session. Once -you have specified your email account, you must enable labeleR -application to have access to your mail. - -To do so, you must first ensure you have installed the `gmailr` package: - -```{r load gmailr, eval = FALSE} -require("gmailr") -``` - -To send emails automatically you have to specify, at least, a google -account within a list. This object must contain a slot named "account", -where the email must be specified. Additionally, you can also specify -the email subject in a slot named "subject", and the body content of the -email in a slot named "body". These latter two are optional, and if not -specified, default values will be applied. - -```{r email list, include = FALSE} -students.table <- data.frame( - "Names" = c("Harry Potter", "Hermione Granger", "Neville Longbottom", "Ginny Weasley"), - "email" = c("h.potter@hogwarts.co.uk", "h.granger@hogwarts.co.uk", - "n.longbottom@hogwarts.co.uk", "g.weasley@hogwarts.co.uk")) - -``` - -Now we have a list of attendees with their emails - -```{r see table} -print(students.table) -``` - -If we specify the email column and our email information, we can send -everything automatically! - -```{r send email, eval=FALSE} - -emailinfo <- list( - "account" = "a.dumbledore@hogwarts.co.uk" - #, "subject" = "Class certificates - School Year 1992-1993" #optional to use non-default values - #, "body" = "Here you have your certificate, dear student!" #optional to use non-default values -) - - - -create_attendance_certificate( - data = students.table, - path = "labeleR_output", - filename = "attendance_certificates", - language = "English" , - name.column = "Names", - - email.column = "email", - email.info = emailinfo, - - type = "class", - title = "Potions (year 1992-1993)", - date = "23/06/1993", - hours = "200", - freetext = "taught by Professor S. Snape", - signer = "A.P.W.B. Dumbledore", - signer.role = "School Headmaster", - rpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), - lpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), - signature.pic = system.file("rmarkdown/pictures/dumbledore.png", package = "labeleR") -) - -``` - -This way, every student will receive an email with their individual certificate attached. ## Citation diff --git a/inst/gmailr/gmailr-token.rds b/inst/gmailr/gmailr-token.rds deleted file mode 100644 index 95c5c73a0181945860c4d35903849b44b02d9f58..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6671 zcmV+q8t~;dDq25m{FTWJBxXA=E28Ml>5ja_rcAwG&|ANziZ_QopM`~T1N-}-1bOWO z>@LPL?zY&X6DEeCxw39c<1v@j)@pvITNqtLYHB9aB=n`MWEFU&6t z>KH|3wZnd%1G*O>DN0LVvWkpnA;rUeKJNI8_Ue}7?g*{^2xACus+dc*i9+uBz%G4^wK;FICVlP7)TIYW_)fenZNn)LDDB zF8g~N0=Q{;)iGHIrOVB>*0n*&3#-ltC^-RVR;MGOzWW25z6HdBEEgErAAf(Hgo@Hm zjfJ^LvoeY-E!t7J*(9wu2d7zED3h|iZmtj}(di9hGH>EmEm;RT$E))=-aIDLNxZNu zllQXoNJVp;L2kP?-MWc-%5~!O;TS3ZUhWrIy{a`ve92d3Z>Z=77g4Z)+#L;ZB=xL@ zI($!WQt=)6llX`8`~%wI_-X#C{_X>B__%k^!hRWpq+i`L-^wNQA&{3(zfu3UhzP< z_2|EF30<{6^{*MG`04bT-!QoU`_P<}Hj{w2H zN-RW+=m-R7eR~Q@d#pF{uXtqNj28@YT4#xcUwTY_9kc_5q3X!SvwaB6*)SpDdXm|q zUKm6{g#{2=@^kS#W+er5L0l4titN0DJ@OiE#^X_~sI2Q_FQ@Qi%aUHo?4(kq*0sOE ztH#)M+y8xY1K`I|6~Tw$-hyTY8kqJagS4JE#6asC;lTb)lk9F`@W_V~Y$@FU-ERS$ zUD#v4n2#f9@{Y&rRl3{ocQDo{cJpkPnu=6_WVSs+md&@qM=y4!;@?aqMQJ6Bio%G} z9w(!=Lyy)+J@HP$a1r$MO-Vo;oGJrm%}1GkUv#uG{(lWrscsldx!PcMr=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ATCH 09/18] reoxygenize --- DESCRIPTION | 2 +- man/create_attendance_certificate.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f70af99..b062a37 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,7 +19,7 @@ Description: Create custom labels, badges, certificates License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 URL: https://github.com/EcologyR/labeleR/, https://ecologyr.github.io/labeleR/ BugReports: https://github.com/EcologyR/labeleR/issues/ Depends: diff --git a/man/create_attendance_certificate.Rd b/man/create_attendance_certificate.Rd index 71f8eac..7a3c163 100644 --- a/man/create_attendance_certificate.Rd +++ b/man/create_attendance_certificate.Rd @@ -39,7 +39,7 @@ create_attendance_certificate( \item{email.column}{Character. Name of the column in \code{data} storing attendees' emails to automatically send them their certificates.} -\item{email.info}{List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. +\item{email.info}{List. Must include at least one slot named 'user' specifing a Google Mail accout to send certificates from. Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email.} \item{type}{Character (optional). Type of event (conference, workshop, seminar...)} From d3857c6091276d3d4a4690a4f543509e7c21654a Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Mon, 11 Nov 2024 15:38:30 +0100 Subject: [PATCH 10/18] enables rendering several of multiple participation certificates for each attendee --- R/certificate_participation.R | 5 +++-- R/zzz.R | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/R/certificate_participation.R b/R/certificate_participation.R index 1f3cbe9..35ea83b 100644 --- a/R/certificate_participation.R +++ b/R/certificate_participation.R @@ -200,8 +200,9 @@ create_participation_certificate <- function( for (i in 1:nrow(data)) { out.name <- filename - out.name <- paste0(out.name, "_", data[i, name.column], "_", - gsub("/","-", data[i, date.column])) + out.name <- paste0(out.name, "_", data[i, name.column]) + out.name <- check_file_name(out.name, ".pdf", path) + output_file <- paste0(out.name,'.pdf') bl.char <- "~" diff --git a/R/zzz.R b/R/zzz.R index 16c3469..62f7ab4 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -97,6 +97,25 @@ use_image <- function(image = NULL, name = NULL, folder = NULL) { } +#Function to change a name between brackets +check_file_name <- function(name, suffix, path){ + + if(!file.exists(paste0(path, "/", name, suffix))){ + newname <- name + }else{ + if(!grepl("\\(", name) & !grepl("\\)", name)){ + newname <- paste0(name, "(2)") + }else{ + p1 <- unlist(gregexec("\\(", name)) + p2 <- unlist(gregexec("\\)", name)) + num <- substr(name, p1+1, p2-1) + num <- as.integer(num) + newname <- paste0(substr(name, 1, p1),num+1 ,")") + } + } + return(newname) +} + #### Function to set the SMTP server sendmail_setup <- function(email.column, email.info){ From 037afc942fdecd0621d68695a9a42f69fd8cd9cf Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Mon, 11 Nov 2024 17:25:47 +0100 Subject: [PATCH 11/18] new email_configuration function --- R/zzz.R | 107 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 24 deletions(-) diff --git a/R/zzz.R b/R/zzz.R index 62f7ab4..5444db9 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -103,19 +103,93 @@ check_file_name <- function(name, suffix, path){ if(!file.exists(paste0(path, "/", name, suffix))){ newname <- name }else{ - if(!grepl("\\(", name) & !grepl("\\)", name)){ + files <- list.files(path, pattern = name) + if(length(files)==1){ newname <- paste0(name, "(2)") }else{ - p1 <- unlist(gregexec("\\(", name)) - p2 <- unlist(gregexec("\\)", name)) - num <- substr(name, p1+1, p2-1) - num <- as.integer(num) - newname <- paste0(substr(name, 1, p1),num+1 ,")") + files <- gsub(name, "", files) + files <- gsub(suffix, "", files) + files <- files[files!=""] + files <- gsub("\\(", "", files) + files <- gsub("\\)", "", files) + num <- max(as.numeric(files)) + newname <- paste0(name, "(",num+1 ,")") } } return(newname) } + +#' @param user Character. Email account used to send certificates. +#' @param app.name Name of the mail application used to send emails. To create one, access `https://myaccount.google.com/apppasswords` +#' @param subject Character. Subject of the email to be sent If not specified, labeleR will use a default value. +#' @param body Character. Body text of the email to be sent. If not specified, labeleR will use a default value. +#' @param cc Character. String (or vector of strings) containing the email addresses to send the email as a copy. +#' @param bcc Character. String (or vector of strings) containing the email addresses to send the email as a hidden copy. +#' +#' @return A list including at least a 'user' string' and an 'app.name' string. Optionally, slots 'subject', +#' 'body', 'cc' and 'bcc' can be edited to compile the email to send. +#' +#' @export +#' +#' @author Ignacio Ramos-Gutierrez, Julia G. de Aledo, Jimena Mateo-Martín, Francisco Rodriguez-Sanchez +#' +email_configuration <- function(user, app.name =NULL, subject = NULL, body = NULL, cc = NULL, bcc = NULL){ + + if (!requireNamespace("keyring", quietly = TRUE)) { + stop("For automatically sending emails, the `keyring` package must be installed.\n", + "Please run install.packages(\"keyring\")") + } + + + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == user,] + + if(!is.null(app.name)){ + blastula::create_smtp_creds_key(id = app.name, + provider = "gmail", + user = user, + overwrite = T) + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == user,] + credentials <- credentials[credentials$id == app.name,] + } + + + + if(nrow(credentials) == 0 | is.null(app.name) ){ + cat( + "You must first create a mail sending application\n(dont't worry, it is very easy, and is necessary only the first time!).\n\n", + + "- First access this link using the specified mail user (R will open it for you): + https://myaccount.google.com/apppasswords \n\n", + + "- Create an application.\n\n", + + "- Save the password anywhere safe, as you will be asked for it later!\n\n") + + utils::browseURL("https://myaccount.google.com/apppasswords") + + stop("Please run again this function specifying your user and application name") + + } + + + email.info.ret <- list( + "user" = user, + "app.name" = app.name, + "subject" = subject, + "body" = body, + "cc"=cc, + "bcc" = bcc + ) + + return(email.info.ret) + + + +} + #### Function to set the SMTP server sendmail_setup <- function(email.column, email.info){ @@ -131,8 +205,7 @@ sendmail_setup <- function(email.column, email.info){ (!inherits(email.info, "list") | !(("user") %in% names(email.info)) | !(("app.name") %in% names(email.info)))){ - stop("'email.info' should be a list including at least two slots named 'user' and 'app.name'.\n" , - "Optionally, slots 'cc', 'bcc', 'subject' and 'body' can also be included") + stop("'email.info' should be a list object created using 'email_configuration()' function") } if(!is.null(email.column) & !is.null(email.info)){ @@ -142,28 +215,14 @@ sendmail_setup <- function(email.column, email.info){ } if (isTRUE(sendmail)) { - sendmail <- utils::askYesNo("You are trying to send automatically certificates via email. Are you sure you want to continue?", + sendmail <- utils::askYesNo("You are trying to send automatically certificates via email. Are you sure you want to continue with the automatic sending?", default = FALSE, prompts = getOption("askYesNo", gettext(c("Yes", "No", "Cancel")))) + if(is.na(sendmail)){stop("Cancel button selected. Aborting.")} } if(isTRUE(sendmail)){ - credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == email.info$user & - credentials$id == email.info$app.name,] - if(nrow(credentials) == 0){ - message("You must create sour mail sending application (dont't worry, it is very easy, and is necessary only the first time!). - -- First access this link using the specified mail user (R will open it for you): -https://myaccount.google.com/apppasswords - -- Create an application. The used application name must be specified in email.info, within the app.name slot. - -- Save the password anywhere safe, as you will be asked for it later!\n\n") - - utils::browseURL("https://myaccount.google.com/apppasswords") - } blastula::create_smtp_creds_key(id = email.info$app.name, provider = "gmail", From 5a8bd8868e5e4397fcd2dc9e34bb331c6b153ceb Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Mon, 11 Nov 2024 17:28:34 +0100 Subject: [PATCH 12/18] automatic email sending functions rearranged --- DESCRIPTION | 1 + NAMESPACE | 1 + R/certificate_attendance.R | 3 +- R/certificate_participation.R | 3 +- R/zzz.R | 4 +++ man/create_attendance_certificate.Rd | 3 +- man/create_participation_certificate.Rd | 3 +- man/email_configuration.Rd | 38 +++++++++++++++++++++++++ 8 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 man/email_configuration.Rd diff --git a/DESCRIPTION b/DESCRIPTION index b062a37..acfdc42 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,6 +27,7 @@ Depends: LazyData: true Suggests: testthat (>= 3.0.0) + keyring Config/testthat/edition: 3 Imports: rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 41e8287..c955e66 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,3 +6,4 @@ export(create_collection_label) export(create_herbarium_label) export(create_participation_certificate) export(create_tiny_label) +export(email_configuration) diff --git a/R/certificate_attendance.R b/R/certificate_attendance.R index a1e72b4..3771ffb 100644 --- a/R/certificate_attendance.R +++ b/R/certificate_attendance.R @@ -8,8 +8,7 @@ #' @param language Character. Select 'English' or 'Spanish'. #' @param name.column Character. Name of the column in `data` storing attendees' name. #' @param email.column Character. Name of the column in `data` storing attendees' emails to automatically send them their certificates. -#' @param email.info List. Must include at least one slot named 'user' specifing a Google Mail accout to send certificates from. -#' Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email. +#' @param email.info Object created using 'email_configuration()' function. #' @param type Character (optional). Type of event (conference, workshop, seminar...) #' @param title Character. Title of the event #' @param date Date of the event diff --git a/R/certificate_participation.R b/R/certificate_participation.R index 35ea83b..1b14199 100644 --- a/R/certificate_participation.R +++ b/R/certificate_participation.R @@ -16,8 +16,7 @@ #' @param date.column Character. Name of the column in `data` storing dates of #' participation. #' @param email.column Character. Name of the column in `data` storing attendees' emails to automatically send them their certificates. -#' @param email.info List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. -#' Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email. +#' @param email.info Object created using 'email_configuration()' function. #' @param type Character (optional). Type of event (conference, workshop, seminar...) #' @param event Character. Title of the event #' @param freetext Character (optional). Free text to insert before the date. diff --git a/R/zzz.R b/R/zzz.R index 5444db9..cf6bf5b 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -120,6 +120,10 @@ check_file_name <- function(name, suffix, path){ } +#' Setup the email server +#' +#' Create the email.info object to feed the certificate creation functions +#' #' @param user Character. Email account used to send certificates. #' @param app.name Name of the mail application used to send emails. To create one, access `https://myaccount.google.com/apppasswords` #' @param subject Character. Subject of the email to be sent If not specified, labeleR will use a default value. diff --git a/man/create_attendance_certificate.Rd b/man/create_attendance_certificate.Rd index 7a3c163..d6f102f 100644 --- a/man/create_attendance_certificate.Rd +++ b/man/create_attendance_certificate.Rd @@ -39,8 +39,7 @@ create_attendance_certificate( \item{email.column}{Character. Name of the column in \code{data} storing attendees' emails to automatically send them their certificates.} -\item{email.info}{List. Must include at least one slot named 'user' specifing a Google Mail accout to send certificates from. -Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email.} +\item{email.info}{Object created using 'email_configuration()' function.} \item{type}{Character (optional). Type of event (conference, workshop, seminar...)} diff --git a/man/create_participation_certificate.Rd b/man/create_participation_certificate.Rd index 759d4ee..7e27f75 100644 --- a/man/create_participation_certificate.Rd +++ b/man/create_participation_certificate.Rd @@ -53,8 +53,7 @@ participation.} \item{email.column}{Character. Name of the column in \code{data} storing attendees' emails to automatically send them their certificates.} -\item{email.info}{List. Must include at least one slot named 'account' specifing a Google Mail accout to send certificates from. -Optionally, other slots named 'subject' and 'body' can be included to specify such parameters in the email.} +\item{email.info}{Object created using 'email_configuration()' function.} \item{type}{Character (optional). Type of event (conference, workshop, seminar...)} diff --git a/man/email_configuration.Rd b/man/email_configuration.Rd new file mode 100644 index 0000000..16f2775 --- /dev/null +++ b/man/email_configuration.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/zzz.R +\name{email_configuration} +\alias{email_configuration} +\title{Setup the email server} +\usage{ +email_configuration( + user, + app.name = NULL, + subject = NULL, + body = NULL, + cc = NULL, + bcc = NULL +) +} +\arguments{ +\item{user}{Character. Email account used to send certificates.} + +\item{app.name}{Name of the mail application used to send emails. To create one, access \verb{https://myaccount.google.com/apppasswords}} + +\item{subject}{Character. Subject of the email to be sent If not specified, labeleR will use a default value.} + +\item{body}{Character. Body text of the email to be sent. If not specified, labeleR will use a default value.} + +\item{cc}{Character. String (or vector of strings) containing the email addresses to send the email as a copy.} + +\item{bcc}{Character. String (or vector of strings) containing the email addresses to send the email as a hidden copy.} +} +\value{ +A list including at least a 'user' string' and an 'app.name' string. Optionally, slots 'subject', +'body', 'cc' and 'bcc' can be edited to compile the email to send. +} +\description{ +Create the email.info object to feed the certificate creation functions +} +\author{ +Ignacio Ramos-Gutierrez, Julia G. de Aledo, Jimena Mateo-Martín, Francisco Rodriguez-Sanchez +} From 8152a5a288f0b65b993a20bed2db6fc7c3f63ab4 Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Mon, 11 Nov 2024 17:34:23 +0100 Subject: [PATCH 13/18] exported function to own script --- R/email_configuration.R | 75 +++++++++++++++++++++++++++++++++++++++ R/zzz.R | 77 +---------------------------------------- 2 files changed, 76 insertions(+), 76 deletions(-) create mode 100644 R/email_configuration.R diff --git a/R/email_configuration.R b/R/email_configuration.R new file mode 100644 index 0000000..a5bbc76 --- /dev/null +++ b/R/email_configuration.R @@ -0,0 +1,75 @@ + + +#' Setup the email server +#' +#' Create the email.info object to feed the certificate creation functions +#' +#' @param user Character. Email account used to send certificates. +#' @param app.name Name of the mail application used to send emails. To create one, access `https://myaccount.google.com/apppasswords` +#' @param subject Character. Subject of the email to be sent If not specified, labeleR will use a default value. +#' @param body Character. Body text of the email to be sent. If not specified, labeleR will use a default value. +#' @param cc Character. String (or vector of strings) containing the email addresses to send the email as a copy. +#' @param bcc Character. String (or vector of strings) containing the email addresses to send the email as a hidden copy. +#' +#' @return A list including at least a 'user' string' and an 'app.name' string. Optionally, slots 'subject', +#' 'body', 'cc' and 'bcc' can be edited to compile the email to send. +#' +#' @export +#' +#' @author Ignacio Ramos-Gutierrez, Julia G. de Aledo, Jimena Mateo-Martín, Francisco Rodriguez-Sanchez +#' +email_configuration <- function(user, app.name =NULL, subject = NULL, body = NULL, cc = NULL, bcc = NULL){ + + if (!requireNamespace("keyring", quietly = TRUE)) { + stop("For automatically sending emails, the `keyring` package must be installed.\n", + "Please run install.packages(\"keyring\")") + } + + + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == user,] + + if(!is.null(app.name)){ + blastula::create_smtp_creds_key(id = app.name, + provider = "gmail", + user = user, + overwrite = T) + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == user,] + credentials <- credentials[credentials$id == app.name,] + } + + + + if(nrow(credentials) == 0 | is.null(app.name) ){ + cat( + "You must first create a mail sending application\n(dont't worry, it is very easy, and is necessary only the first time!).\n\n", + + "- First access this link using the specified mail user (R will open it for you): + https://myaccount.google.com/apppasswords \n\n", + + "- Create an application.\n\n", + + "- Save the password anywhere safe, as you will be asked for it later!\n\n") + + utils::browseURL("https://myaccount.google.com/apppasswords") + + stop("Please run again this function specifying your user and application name") + + } + + + email.info.ret <- list( + "user" = user, + "app.name" = app.name, + "subject" = subject, + "body" = body, + "cc"=cc, + "bcc" = bcc + ) + + return(email.info.ret) + + + +} diff --git a/R/zzz.R b/R/zzz.R index cf6bf5b..ba30d1f 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -119,81 +119,6 @@ check_file_name <- function(name, suffix, path){ return(newname) } - -#' Setup the email server -#' -#' Create the email.info object to feed the certificate creation functions -#' -#' @param user Character. Email account used to send certificates. -#' @param app.name Name of the mail application used to send emails. To create one, access `https://myaccount.google.com/apppasswords` -#' @param subject Character. Subject of the email to be sent If not specified, labeleR will use a default value. -#' @param body Character. Body text of the email to be sent. If not specified, labeleR will use a default value. -#' @param cc Character. String (or vector of strings) containing the email addresses to send the email as a copy. -#' @param bcc Character. String (or vector of strings) containing the email addresses to send the email as a hidden copy. -#' -#' @return A list including at least a 'user' string' and an 'app.name' string. Optionally, slots 'subject', -#' 'body', 'cc' and 'bcc' can be edited to compile the email to send. -#' -#' @export -#' -#' @author Ignacio Ramos-Gutierrez, Julia G. de Aledo, Jimena Mateo-Martín, Francisco Rodriguez-Sanchez -#' -email_configuration <- function(user, app.name =NULL, subject = NULL, body = NULL, cc = NULL, bcc = NULL){ - - if (!requireNamespace("keyring", quietly = TRUE)) { - stop("For automatically sending emails, the `keyring` package must be installed.\n", - "Please run install.packages(\"keyring\")") - } - - - credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == user,] - - if(!is.null(app.name)){ - blastula::create_smtp_creds_key(id = app.name, - provider = "gmail", - user = user, - overwrite = T) - credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == user,] - credentials <- credentials[credentials$id == app.name,] - } - - - - if(nrow(credentials) == 0 | is.null(app.name) ){ - cat( - "You must first create a mail sending application\n(dont't worry, it is very easy, and is necessary only the first time!).\n\n", - - "- First access this link using the specified mail user (R will open it for you): - https://myaccount.google.com/apppasswords \n\n", - - "- Create an application.\n\n", - - "- Save the password anywhere safe, as you will be asked for it later!\n\n") - - utils::browseURL("https://myaccount.google.com/apppasswords") - - stop("Please run again this function specifying your user and application name") - - } - - - email.info.ret <- list( - "user" = user, - "app.name" = app.name, - "subject" = subject, - "body" = body, - "cc"=cc, - "bcc" = bcc - ) - - return(email.info.ret) - - - -} - #### Function to set the SMTP server sendmail_setup <- function(email.column, email.info){ @@ -202,7 +127,7 @@ sendmail_setup <- function(email.column, email.info){ } if(!is.null(email.column) & is.null(email.info)){ - stop("You must specify your email information") + stop("You must specify an email.info object.\nUse function 'email_configuration()' to create it.") } if(!is.null(email.info) & From 78e289e076f1dfd42bf37a861f7147e88e122a2f Mon Sep 17 00:00:00 2001 From: Francisco Rodriguez-Sanchez Date: Tue, 12 Nov 2024 14:14:09 +0100 Subject: [PATCH 14/18] revised sending emails --- DESCRIPTION | 6 +- NAMESPACE | 2 +- R/certificate_attendance.R | 5 +- R/certificate_participation.R | 5 +- R/configure_email.R | 112 ++++++++++++++++++ R/email_configuration.R | 75 ------------ R/zzz.R | 32 ++--- ...il_configuration.Rd => configure_email.Rd} | 33 ++++-- man/create_attendance_certificate.Rd | 5 +- man/create_participation_certificate.Rd | 5 +- .../testthat/test-certificate_participation.R | 8 +- 11 files changed, 171 insertions(+), 117 deletions(-) create mode 100644 R/configure_email.R delete mode 100644 R/email_configuration.R rename man/{email_configuration.Rd => configure_email.Rd} (54%) diff --git a/DESCRIPTION b/DESCRIPTION index acfdc42..860b67f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,9 +26,9 @@ Depends: R (>= 3.5.0) LazyData: true Suggests: + blastula, + keyring, testthat (>= 3.0.0) - keyring Config/testthat/edition: 3 Imports: - rmarkdown, - blastula + rmarkdown diff --git a/NAMESPACE b/NAMESPACE index c955e66..89984ca 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,9 +1,9 @@ # Generated by roxygen2: do not edit by hand +export(configure_email) export(create_attendance_certificate) export(create_badge) export(create_collection_label) export(create_herbarium_label) export(create_participation_certificate) export(create_tiny_label) -export(email_configuration) diff --git a/R/certificate_attendance.R b/R/certificate_attendance.R index 3771ffb..bf58139 100644 --- a/R/certificate_attendance.R +++ b/R/certificate_attendance.R @@ -7,8 +7,9 @@ #' @param filename Character. Filename of the pdf. If NULL, default is "Attendance" for English, "Asistencia" for Spanish". #' @param language Character. Select 'English' or 'Spanish'. #' @param name.column Character. Name of the column in `data` storing attendees' name. -#' @param email.column Character. Name of the column in `data` storing attendees' emails to automatically send them their certificates. -#' @param email.info Object created using 'email_configuration()' function. +#' @param email.column Character. Name of the column in `data` storing attendees' email address +#' to automatically send them their certificates. +#' @param email.info Object created using [configure_email()] function. #' @param type Character (optional). Type of event (conference, workshop, seminar...) #' @param title Character. Title of the event #' @param date Date of the event diff --git a/R/certificate_participation.R b/R/certificate_participation.R index 1b14199..db69188 100644 --- a/R/certificate_participation.R +++ b/R/certificate_participation.R @@ -15,8 +15,9 @@ #' contribution. #' @param date.column Character. Name of the column in `data` storing dates of #' participation. -#' @param email.column Character. Name of the column in `data` storing attendees' emails to automatically send them their certificates. -#' @param email.info Object created using 'email_configuration()' function. +#' @param email.column Character. Name of the column in `data` storing attendees' email address +#' to automatically send them their certificates. +#' @param email.info Object created using [configure_email()] function. #' @param type Character (optional). Type of event (conference, workshop, seminar...) #' @param event Character. Title of the event #' @param freetext Character (optional). Free text to insert before the date. diff --git a/R/configure_email.R b/R/configure_email.R new file mode 100644 index 0000000..9b414e7 --- /dev/null +++ b/R/configure_email.R @@ -0,0 +1,112 @@ + + +#' Configure email sending +#' +#' Configure the email application to automatically send certificates. +#' +#' @param user Character. Gmail account that will be used to send certificates. +#' @param app.name Name of the mail application used to send emails. +#' To create one, access . +#' @param subject Character. Subject of the email to be sent. If not specified, labeleR will use a default value. +#' @param body Character. Body text of the email to be sent. If not specified, labeleR will use a default value. +#' @param cc Character. String (or vector of strings) containing the email addresses to send the email as a copy. +#' @param bcc Character. String (or vector of strings) containing the email addresses to send the email as a hidden copy. +#' +#' @return A list including at least a 'user' string' and an 'app.name' string. Optionally, slots 'subject', +#' 'body', 'cc' and 'bcc' can be edited to compile the email to send. +#' +#' @export +#' @examples +#' \dontrun{ +#' email.info <- configure_email(user = 'example@@gmail.com') +#' +#' ## If you already have created an application: +#' email.info <- configure_email(user = 'example@@gmail.com', app.name = "emailsend") +#' +#' } +#' +#' +#' +#' @author Ignacio Ramos-Gutierrez, Julia G. de Aledo, Jimena Mateo-Martín, Francisco Rodriguez-Sanchez +#' +configure_email <- function(user = NULL, + app.name = NULL, + subject = NULL, + body = NULL, + cc = NULL, + bcc = NULL +) { + + if (!requireNamespace("blastula", quietly = TRUE)) { + stop("For automatically sending emails, the `blastula` package must be installed.\n", + "Please run install.packages(\"blastula\")") + } + + if (!requireNamespace("keyring", quietly = TRUE)) { + stop("For automatically sending emails, the `keyring` package must be installed.\n", + "Please run install.packages(\"keyring\")") + } + + if (is.null(user)) { + user <- readline("Please write here the gmail account you would like to use to send the emails: ") + user <- gsub(" ", "", user) + } + stopifnot(is.character(user)) + if (!grepl("@gmail", user)) { + stop("Please provide a complete gmail address") + } + + + credentials <- blastula::view_credential_keys() + credentials <- credentials[credentials$username == user,] + + if (!is.null(app.name)){ + credentials <- credentials[credentials$id == app.name, ] + if(nrow(credentials) == 0){ + stop( "No application ", app.name, " found for user ", user) + } + } + + if (is.null(app.name) ){ + app.exists <- utils::askYesNo("Have you already created an application for this gmail account?", default = FALSE) + if (!isTRUE(app.exists)) { + message( + "You must first create a mail sending application\n(don't worry, it is very easy, and is necessary only the first time!).\n\n", + + "- First access this link using the specified mail user (R will open it for you): + https://myaccount.google.com/apppasswords \n\n", + + "- Choose a name for you application.\n\n", + + "- Save the password anywhere safe, as you will be asked for it later!\n\n") + + utils::browseURL("https://myaccount.google.com/apppasswords") + + } + + app.name <- readline("What is your application name? Please write it here: ") + app.name <- gsub(" ", "", app.name) + + } + + + blastula::create_smtp_creds_key(id = app.name, + provider = "gmail", + user = user, + overwrite = TRUE) + # credentials <- blastula::view_credential_keys() + # credentials <- credentials[credentials$username == user,] + # credentials <- credentials[credentials$id == app.name,] + + email.info.ret <- list( + "user" = user, + "app.name" = app.name, + "subject" = subject, + "body" = body, + "cc" = cc, + "bcc" = bcc + ) + + return(email.info.ret) + +} diff --git a/R/email_configuration.R b/R/email_configuration.R deleted file mode 100644 index a5bbc76..0000000 --- a/R/email_configuration.R +++ /dev/null @@ -1,75 +0,0 @@ - - -#' Setup the email server -#' -#' Create the email.info object to feed the certificate creation functions -#' -#' @param user Character. Email account used to send certificates. -#' @param app.name Name of the mail application used to send emails. To create one, access `https://myaccount.google.com/apppasswords` -#' @param subject Character. Subject of the email to be sent If not specified, labeleR will use a default value. -#' @param body Character. Body text of the email to be sent. If not specified, labeleR will use a default value. -#' @param cc Character. String (or vector of strings) containing the email addresses to send the email as a copy. -#' @param bcc Character. String (or vector of strings) containing the email addresses to send the email as a hidden copy. -#' -#' @return A list including at least a 'user' string' and an 'app.name' string. Optionally, slots 'subject', -#' 'body', 'cc' and 'bcc' can be edited to compile the email to send. -#' -#' @export -#' -#' @author Ignacio Ramos-Gutierrez, Julia G. de Aledo, Jimena Mateo-Martín, Francisco Rodriguez-Sanchez -#' -email_configuration <- function(user, app.name =NULL, subject = NULL, body = NULL, cc = NULL, bcc = NULL){ - - if (!requireNamespace("keyring", quietly = TRUE)) { - stop("For automatically sending emails, the `keyring` package must be installed.\n", - "Please run install.packages(\"keyring\")") - } - - - credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == user,] - - if(!is.null(app.name)){ - blastula::create_smtp_creds_key(id = app.name, - provider = "gmail", - user = user, - overwrite = T) - credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == user,] - credentials <- credentials[credentials$id == app.name,] - } - - - - if(nrow(credentials) == 0 | is.null(app.name) ){ - cat( - "You must first create a mail sending application\n(dont't worry, it is very easy, and is necessary only the first time!).\n\n", - - "- First access this link using the specified mail user (R will open it for you): - https://myaccount.google.com/apppasswords \n\n", - - "- Create an application.\n\n", - - "- Save the password anywhere safe, as you will be asked for it later!\n\n") - - utils::browseURL("https://myaccount.google.com/apppasswords") - - stop("Please run again this function specifying your user and application name") - - } - - - email.info.ret <- list( - "user" = user, - "app.name" = app.name, - "subject" = subject, - "body" = body, - "cc"=cc, - "bcc" = bcc - ) - - return(email.info.ret) - - - -} diff --git a/R/zzz.R b/R/zzz.R index ba30d1f..aa7d9f2 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -127,14 +127,14 @@ sendmail_setup <- function(email.column, email.info){ } if(!is.null(email.column) & is.null(email.info)){ - stop("You must specify an email.info object.\nUse function 'email_configuration()' to create it.") + email.info <- configure_email() } if(!is.null(email.info) & (!inherits(email.info, "list") | !(("user") %in% names(email.info)) | !(("app.name") %in% names(email.info)))){ - stop("'email.info' should be a list object created using 'email_configuration()' function") + stop("'email.info' should be a list object created using 'configure_email' function") } if(!is.null(email.column) & !is.null(email.info)){ @@ -150,18 +150,18 @@ sendmail_setup <- function(email.column, email.info){ if(is.na(sendmail)){stop("Cancel button selected. Aborting.")} } - if(isTRUE(sendmail)){ - - - blastula::create_smtp_creds_key(id = email.info$app.name, - provider = "gmail", - user = email.info$user, - overwrite = T) - credentials <- blastula::view_credential_keys() - credentials <- credentials[credentials$username == email.info$user & - credentials$id == email.info$app.name,] - - } + # if(isTRUE(sendmail)){ + # + # + # blastula::create_smtp_creds_key(id = email.info$app.name, + # provider = "gmail", + # user = email.info$user, + # overwrite = T) + # credentials <- blastula::view_credential_keys() + # credentials <- credentials[credentials$username == email.info$user & + # credentials$id == email.info$app.name,] + # + # } return(sendmail) } @@ -173,8 +173,8 @@ send_mail <- function(data, row, email.info, attachment){ mail.to <- data[row,email.column] - if(is.na(mail.to)){message("email not sent to ", data[row, name.column])}#se puede mandar un auto mensaje?? - if(!is.na(mail.to)){ + if (!grepl("@", mail.to)) {message("email not sent to ", data[row, name.column])}#se puede mandar un auto mensaje?? + if (grepl("@", mail.to)){ mail.from <- email.info$user mail.subj <- email.info$subject diff --git a/man/email_configuration.Rd b/man/configure_email.Rd similarity index 54% rename from man/email_configuration.Rd rename to man/configure_email.Rd index 16f2775..dd8b519 100644 --- a/man/email_configuration.Rd +++ b/man/configure_email.Rd @@ -1,11 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/zzz.R -\name{email_configuration} -\alias{email_configuration} -\title{Setup the email server} +% Please edit documentation in R/configure_email.R +\name{configure_email} +\alias{configure_email} +\title{Configure email sending} \usage{ -email_configuration( - user, +configure_email( + user = NULL, app.name = NULL, subject = NULL, body = NULL, @@ -14,11 +14,12 @@ email_configuration( ) } \arguments{ -\item{user}{Character. Email account used to send certificates.} +\item{user}{Character. Gmail account that will be used to send certificates.} -\item{app.name}{Name of the mail application used to send emails. To create one, access \verb{https://myaccount.google.com/apppasswords}} +\item{app.name}{Name of the mail application used to send emails. +To create one, access \url{https://myaccount.google.com/apppasswords}.} -\item{subject}{Character. Subject of the email to be sent If not specified, labeleR will use a default value.} +\item{subject}{Character. Subject of the email to be sent. If not specified, labeleR will use a default value.} \item{body}{Character. Body text of the email to be sent. If not specified, labeleR will use a default value.} @@ -31,7 +32,19 @@ A list including at least a 'user' string' and an 'app.name' string. Optionally, 'body', 'cc' and 'bcc' can be edited to compile the email to send. } \description{ -Create the email.info object to feed the certificate creation functions +Configure the email application to automatically send certificates. +} +\examples{ +\dontrun{ +email.info <- configure_email(user = 'example@gmail.com') + +## If you already have created an application: +email.info <- configure_email(user = 'example@gmail.com', app.name = "emailsend") + +} + + + } \author{ Ignacio Ramos-Gutierrez, Julia G. de Aledo, Jimena Mateo-Martín, Francisco Rodriguez-Sanchez diff --git a/man/create_attendance_certificate.Rd b/man/create_attendance_certificate.Rd index d6f102f..3549bd8 100644 --- a/man/create_attendance_certificate.Rd +++ b/man/create_attendance_certificate.Rd @@ -37,9 +37,10 @@ create_attendance_certificate( \item{name.column}{Character. Name of the column in \code{data} storing attendees' name.} -\item{email.column}{Character. Name of the column in \code{data} storing attendees' emails to automatically send them their certificates.} +\item{email.column}{Character. Name of the column in \code{data} storing attendees' email address +to automatically send them their certificates.} -\item{email.info}{Object created using 'email_configuration()' function.} +\item{email.info}{Object created using \code{\link[=configure_email]{configure_email()}} function.} \item{type}{Character (optional). Type of event (conference, workshop, seminar...)} diff --git a/man/create_participation_certificate.Rd b/man/create_participation_certificate.Rd index 7e27f75..f66f09f 100644 --- a/man/create_participation_certificate.Rd +++ b/man/create_participation_certificate.Rd @@ -51,9 +51,10 @@ contribution.} \item{date.column}{Character. Name of the column in \code{data} storing dates of participation.} -\item{email.column}{Character. Name of the column in \code{data} storing attendees' emails to automatically send them their certificates.} +\item{email.column}{Character. Name of the column in \code{data} storing attendees' email address +to automatically send them their certificates.} -\item{email.info}{Object created using 'email_configuration()' function.} +\item{email.info}{Object created using \code{\link[=configure_email]{configure_email()}} function.} \item{type}{Character (optional). Type of event (conference, workshop, seminar...)} diff --git a/tests/testthat/test-certificate_participation.R b/tests/testthat/test-certificate_participation.R index c08f688..ee074d6 100644 --- a/tests/testthat/test-certificate_participation.R +++ b/tests/testthat/test-certificate_participation.R @@ -29,8 +29,8 @@ test_that("PDF certificates are created", { signer.role = "Head" ) - expect_true(file.exists(file.path(path, "Participacion_Pippin_02-06-2016.pdf"))) - expect_true(file.exists(file.path(path, "Participacion_Merry_2016-04-03.pdf"))) + expect_true(file.exists(file.path(path, "Participacion_Pippin.pdf"))) + expect_true(file.exists(file.path(path, "Participacion_Merry.pdf"))) ## English @@ -48,8 +48,8 @@ test_that("PDF certificates are created", { signer.role = "Head" ) - expect_true(file.exists(file.path(path, "Participation_Pippin_02-06-2016.pdf"))) - expect_true(file.exists(file.path(path, "Participation_Merry_2016-04-03.pdf"))) + expect_true(file.exists(file.path(path, "Participation_Pippin.pdf"))) + expect_true(file.exists(file.path(path, "Participation_Merry.pdf"))) }) From 333b5cb7c099c2ecae52651956b568ff5e54f44e Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Tue, 12 Nov 2024 15:04:34 +0100 Subject: [PATCH 15/18] footer message corrected --- R/zzz.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/zzz.R b/R/zzz.R index aa7d9f2..718a6ce 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -191,7 +191,7 @@ send_mail <- function(data, row, email.info, email <- blastula::compose_email( body = blastula::md(mail.body), footer = blastula::md( - "Mail sent on automatically using labeleR.\r\n + "Mail sent automatically using labeleR.\r\n https://ecologyr.github.io/labeleR/")) email <- blastula::add_attachment(email, file = attachment) From 8e06c5bc8837a21e4ac205a2e6723f9abc741fb2 Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Wed, 13 Nov 2024 10:24:01 +0100 Subject: [PATCH 16/18] avoid overwriting of attendance files, mail composing allowing paragraphs --- R/certificate_attendance.R | 1 + R/zzz.R | 1 + 2 files changed, 2 insertions(+) diff --git a/R/certificate_attendance.R b/R/certificate_attendance.R index bf58139..e886efe 100644 --- a/R/certificate_attendance.R +++ b/R/certificate_attendance.R @@ -186,6 +186,7 @@ create_attendance_certificate <- function( for (i in 1:nrow(data)) { out.name <- filename out.name <- paste0(out.name, "_", data[i, name.column]) + out.name <- check_file_name(out.name, ".pdf", path) output_file <- paste0(out.name, '.pdf') bl.char <- "~" diff --git a/R/zzz.R b/R/zzz.R index 718a6ce..cf33936 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -188,6 +188,7 @@ send_mail <- function(data, row, email.info, "This certificate was automatically sent by labeleR using 'blastula'")} + mail.body <- gsub("\n", "\n\n", mail.body) email <- blastula::compose_email( body = blastula::md(mail.body), footer = blastula::md( From e1136cab80de2e598e7a34b17eccd432e8a5cb3e Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Wed, 13 Nov 2024 14:43:31 +0100 Subject: [PATCH 17/18] readme gmailr erased --- README.md | 172 +++++++++++++++++++++++++----------------------------- 1 file changed, 78 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index 5820566..955a90c 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ specifying the Google Sheet URL: ``` r library(gsheet) +#> Warning: package 'gsheet' was built under R version 4.4.2 #URL: https://docs.google.com/spreadsheets/d/1inkk3_oNvvt8ajdK4wOkSgPoUyE8JzENrZgSTFJEFBw/edit#gid=0 people_list_long <- gsheet2tbl("1inkk3_oNvvt8ajdK4wOkSgPoUyE8JzENrZgSTFJEFBw") @@ -209,9 +210,26 @@ variable parameters (such as speaker, title and type of communication, etc.). As well as the attendance certificate, these documents can be rendered in English and in Spanish. -| ![Participation certificate (blank)](man/figures/Participation_blank.png) | -|---------------------------------------------------------------------------| -| | + +++ + + + + + + + + + + +
+ + +
#### Participation certificate example: @@ -244,9 +262,25 @@ create_participation_certificate( In this example, each certificate will be rendered in an individual PDF document. -| ![Participation certificate](man/figures/Participation_certificates.png) | -|--------------------------------------------------------------------------| -| | + +++ + + + + + + + + + + +
+ + +
#### **NOTE:** To see how to send certificates automatically *via* email, go to the FAQ section! @@ -354,9 +388,20 @@ As a novelty, the user may manually fix the backgroud and text colors to their preference, using HTML color codes (same code as HEX, but without the ‘\#’). By default, background colors are two hues of green. -| | -|-----------------------------------------------------------------------| -| ![Collection labels (blank)](man/figures/collection_labels_blank.png) | + +++ + + + + + +
+ + +
#### Collection labels example: @@ -431,8 +476,9 @@ This implementation should be used just to change text formats in only a part of values stored in columns (variable parameters). To do so, you must edit the cell value, specifying where the italics -text must start with `\\textit`, and `\\end` where it ends; and -`\\textbf` followed by `\\end` for bold text. In case you want to +text must start with `\\textit` (or `\\emph`, which allows to open +italics in a reular text, and vice-versa), and `\\end` where it ends; +and `\\textbf` followed by `\\end` for bold text. In case you want to combine both, you will have to specify `\\end` twice. For example, this could be helpful to include italics in a species name @@ -470,95 +516,33 @@ create_participation_certificate( ``` -| ![Custom italics example](man/figures/Participation_certificate_italics.png) | -|------------------------------------------------------------------------------| -| | - -### Sending certificates automatically using `gmailr` - -Both attendance and participation certificates can be sent automatically -using a gmail account. All you have to do is to specify a mail from -which you want to send them (it will open a connection through an -authentication step), and specify a column name where the email -recipient’s accounts are stored. - -**NOTE**: The account to be used will be asked only once in each -session. To change the sending account, you must restart R session. Once -you have specified your email account, you must enable labeleR -application to have access to your mail. - -To do so, you must first ensure you have installed the `gmailr` package: - -``` r -require("gmailr") -``` - -To send emails automatically you have to specify, at least, a google -account within a list. This object must contain a slot named “account”, -where the email must be specified. Additionally, you can also specify -the email subject in a slot named “subject”, and the body content of the -email in a slot named “body”. These latter two are optional, and if not -specified, default values will be applied. - -Now we have a list of attendees with their emails - -``` r -print(students.table) -#> Names email -#> 1 Harry Potter h.potter@hogwarts.co.uk -#> 2 Hermione Granger h.granger@hogwarts.co.uk -#> 3 Neville Longbottom n.longbottom@hogwarts.co.uk -#> 4 Ginny Weasley g.weasley@hogwarts.co.uk -``` - -If we specify the email column and our email information, we can send -everything automatically! - -``` r - -emailinfo <- list( - "account" = "a.dumbledore@hogwarts.co.uk" - #, "subject" = "Class certificates - School Year 1992-1993" #optional to use non-default values - #, "body" = "Here you have your certificate, dear student!" #optional to use non-default values -) - - - -create_attendance_certificate( - data = students.table, - path = "labeleR_output", - filename = "attendance_certificates", - language = "English" , - name.column = "Names", - - email.column = "email", - email.info = emailinfo, - - type = "class", - title = "Potions (year 1992-1993)", - date = "23/06/1993", - hours = "200", - freetext = "taught by Professor S. Snape", - signer = "A.P.W.B. Dumbledore", - signer.role = "School Headmaster", - rpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), - lpic = system.file("rmarkdown/pictures/Hogwarts_logo.png", package = "labeleR"), - signature.pic = system.file("rmarkdown/pictures/dumbledore.png", package = "labeleR") -) - -``` - -This way, every student will receive an email with their individual -certificate attached. + +++ + + + + + + + + + + +
+ + +
## Citation ``` r citation("labeleR") -#> #> To cite package 'labeleR' in publications use: #> -#> Ramos-Gutierrez I, de Aledo JG, Rodríguez-Sánchez F (2023). _labeleR: +#> Ramos-Gutierrez I, de Aledo JG, Rodríguez-Sánchez F (2024). _labeleR: #> Automate the Production of Custom Labels, Badges, Certificates, and #> Other Documents_. . #> @@ -567,7 +551,7 @@ citation("labeleR") #> @Manual{, #> title = {labeleR: Automate the Production of Custom Labels, Badges, Certificates, and Other Documents}, #> author = {Ignacio Ramos-Gutierrez and Julia G. {de Aledo} and Francisco Rodríguez-Sánchez}, -#> year = {2023}, +#> year = {2024}, #> url = {https://EcologyR.github.io/labeleR/}, #> } ``` From 1840de125a1675f08fd97ebbee5907a9a9ed5a6f Mon Sep 17 00:00:00 2001 From: iramosgutierrez Date: Wed, 13 Nov 2024 14:45:26 +0100 Subject: [PATCH 18/18] configure email added in yaml [skip ci] --- _pkgdown.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index a73141e..10089d5 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -8,6 +8,7 @@ reference: - create_herbarium_label - create_collection_label - create_tiny_label + - configure_email - title: Example datasets contents: