A wrapper function that sends an email (via sendmailR) reporting the outcome of another function
Source:R/logging.R
send_email.Rd
This function sends an email via `sendmailR`, optionally including a dataframe(s) or zip files(s) as attachments.
Usage
send_email(
email_body,
email_subject = "",
email_to = "",
email_cc = "",
email_from = "",
df_to_email = NULL,
file_name = NULL,
...
)
Arguments
- email_body
The contents of the email
- email_subject
The subject line of the email
- email_to
The email addresses of the primary recipient(s), separate recipient addresses with spaces
- email_cc
The email addresses of cc'd recipient(s), separate recipient addresses with spaces
- email_from
The email addresses of the sender
- df_to_email
(Optional) A dataframe or a list of dataframes to be included as file attachment(s). If this parameter is used, `file_name` must also be specified. Each dataframe in the list must have a corresponding file name in the `file_name` parameter to ensure a one-to-one match between dataframes and file names.
- file_name
(Optional) A character vector specifying the file name(s) of the attachment(s). Valid file extensions are `.csv`, `.xlsx`, `.zip` and, `.txt`. Each file name must be unique.
- ...
Additional arguments passed directly to the file writing functions: `write.csv` for CSV files, and `writexl::write_xlsx` for XLSX files.
Examples
if (FALSE) { # \dontrun{
email_body <- paste("Failed REDCap data import to", project_title,
"\nThe reason given was:", error_message)
email_subject <- paste("FAILED |", script_name, "|",
Sys.getenv("INSTANCE"), "|", script_run_time)
# email without attachemnts
send_email(email_body, email_subject)
email_to <- c("email1@example.com email2@example.com")
dfs_to_email <- list(head(cars), tail(cars))
file_names <- c("file1.csv", "file2.xlsx")
# single attachment and at least one email address
send_email(
email_subject = email_subject,
email_body = email_body,
email_from = email_from,
email_to = email_to,
df_to_email = head(cars),
file_name = "file1.csv"
)
# multiple attachments and at least one email address
send_email(
email_subject = email_subject,
email_body = email_body,
email_from = email_from,
email_to = email_to,
df_to_email = dfs_to_email,
file_name = file_names
)
send_email(
email_subject = email_subject,
email_body = email_body,
email_from = email_from,
email_to = email_to,
file_name = c("file1.zip", "<path_to_file>file2.zip")
)
# single attachment for each email group
email_to <- c("email1@example.com", c("email2@example.com email3@example.com"))
args_list <- list(
email_subject = email_subject,
email_body = email_body,
email_to = email_to,
email_from = email_from,
df_to_email = dfs_to_email,
file_name = file_names
)
purrr::pmap(args_list, send_email)
# multiple attachments for each email group
email_to <- c(
c("email1@example.com email2@example.com"),
c("email3@example.com email4@example.com")
)
args_list <- list(
email_subject = email_subject,
email_body = email_body,
email_to = email_to,
email_from = email_from,
df_to_email = list(dfs_to_email, dfs_to_email),
file_name = list(file_names, file_names)
)
purrr::pmap(args_list, send_email)
} # }