Last active
October 19, 2024 04:11
-
-
Save MarkEdmondson1234/ddcac436cbdfd4557639522573bfc7b6 to your computer and use it in GitHub Desktop.
Send an email via an R function using Mailgun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Email a user a report is ready | |
#' | |
#' Requires an account at Mailgun: https://mailgun.com | |
#' Pre-verification can only send to a whitelist of emails you configure | |
#' | |
#' @param email Email to send to | |
#' @param mail_message Any extra info | |
#' | |
#' @return TRUE if successful email sent | |
#' @import httr | |
#' @export | |
sendEmail <- function(email = "[email protected]", | |
mail_message = "Hello"){ | |
url <- "https://api.mailgun.net/v3/sandbox5f2XXXXXXXa.mailgun.org/messages" | |
## username:password so api_key is all after the api: | |
api_key <- "key-c5957XXXXXXXXXXXbb9cf8ce" | |
the_body <- | |
list( | |
from="Mailgun Sandbox <[email protected]>", | |
to=email, | |
subject="Mailgun from R", | |
text=mail_message | |
) | |
req <- httr::POST(url, | |
httr::authenticate("api", api_key), | |
encode = "form", | |
body = the_body) | |
httr::stop_for_status(req) | |
TRUE | |
} |
Pimped it up a little: https://github.com/laresbernardo/lares/blob/master/R/mails.R
Just need to know how to send multiple attachments for it to fully work!
Sorry I missed your comment @laresbernardo, they only just started sending notifications.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello!! Really enjoyed this function because I want to get rid of
mailR::send.mail()
that depends onrJava
. But, how can I send attachments with PDFs, ggplots objects, CSVs, and simple HTML files asmailR
does? Can you please share a reproducible example? Already have mi API credentials and is working just fine with simple texts.I think it must have something to do with
add_headers("Content-Type" = "multipart/form-data")
in thePOST
function, and something likepdf = upload_file("~/Desktop/myPDF.pdf")
inside thethe_body
list. But haven't succeededUPDATE: I got to send an attachment file with what I commented above. But can't send more than one in the same email! Is it possible? tried a list for
attachment
inside thethe_body
but doesn't work.