Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Send an email via an R function using Mailgun
#' Email a user a report is ready
#' Requires an account at Mailgun:
#' 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 = "",
mail_message = "Hello"){
url <- ""
## username:password so api_key is all after the api:
api_key <- "key-c5957XXXXXXXXXXXbb9cf8ce"
the_body <-
from="Mailgun Sandbox <>",
subject="Mailgun from R",
req <- httr::POST(url,
httr::authenticate("api", api_key),
encode = "form",
body = the_body)

IronistM commented Apr 13, 2017

I think that at line:23 value mailmessage should be mail_message.


MarkEdmondson1234 commented Jun 7, 2017

Ah, thanks!

teguhn commented Dec 4, 2017

how do you send attachments? thanks

For email attachments, modify above to include HTML files - see the API docs


 the_body <-
      from = from_name,
      to = email,
      subject = subject_line,
      html = html_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment