send = function(mail) {
tar <- "https://api.sendgrid.com/v3/mail/send"
key = Sys.getenv("SENDGRID_API")
if (nchar(key) == 0) key = auth_key()
ahd <- httr::add_headers(Authorization = paste0("Bearer ", key), `content-type` = "application/json")
body <- jsonlite::toJSON(mail, auto_unbox = TRUE)
body <- gsub("personalizations\":", "personalizations\":[",
body)
body <- gsub("},\"subject\"", "}],\"subject\"", body)
res <- httr::POST(tar, ahd, body = body)
res <- httr::content(res)
if (identical(res, raw(0))) {
res <- list(success = "Send Success!")
}
return(res)
}