mailslurp-examples - rlang-email-sending-in-r

https://github.com/mailslurp/examples

Table of Contents

rlang-email-sending-in-r/main.r

#<gen>r_import_sendmailr
library(httr)
library(sendmailR)
#</gen>

print("Running rlang email")

#<gen>r_smtp_api
# check api key for mailslurp
api_key <- Sys.getenv("API_KEY", "")
if (!nchar(api_key)) {
  print("ERROR = Missing API_KEY")
  stop
}
#</gen>

#<gen>r_smtp_fetch
# make http request to mailslurp to obtain smtp access
print("Fetching smtp access details")
r <- GET("https://api.mailslurp.com/inboxes/imap-smtp-access", add_headers("x-api-key" = api_key))
status <- status_code(r)
access_details <- content(r)
if (status < 200 || status > 299) {
  print(paste("ERROR", "Get request to smtp access failed with status", status, "body", body))
  stop
}
#</gen>

#<gen>r_smtp_host
# extract smtp authentication details from response
host <- access_details$smtpServerHost
port <- access_details$smtpServerPort
username <- access_details$smtpUsername
password <- access_details$smtpPassword
#</gen>

#<gen>r_smtp_inbox
# get an inbox to send with
print("Fetching inbox details")
r <- GET("https://api.mailslurp.com/inboxes/paginated?page=0&size=1", add_headers("x-api-key" = api_key))
status <- status_code(r)
inbox_list <- content(r)
if (status < 200 || status > 299) {
  print(paste("ERROR", "Get request to inboxes failed with status", status, "body", body))
  stop
}
# now extract the email address from the result
email_address <- inbox_list$content[[1]]$emailAddress
address = paste("<", email_address, ">", sep="")
print (paste("Found email address:", address))
#</gen>


#<gen>r_smtp_send
# Now send an email to the address
from <- "<my@sender.com>" 
to <- address
subject <- "Send email with R!"
body <- "Wow, R can do everything."
sendmail(from,to,subject,body,control=list(smtpServer=host,smtpPort=port))
#</gen>

rlang-email-sending-in-r/main-auth.r

#<gen>r_import_blastula
library(httr)
library(blastula)
#</gen>

print("Running rlang email")

#<gen>r_api_key
# check api key for mailslurp
api_key <- Sys.getenv("API_KEY", "")
if (!nchar(api_key)) {
  print("ERROR = Missing API_KEY")
  stop
}
#</gen>

#<gen>r_fetch_details
# make http request to mailslurp to obtain smtp access
print("Fetching smtp access details")
r <- GET("https://api.mailslurp.com/inboxes/imap-smtp-access", add_headers("x-api-key" = api_key))
status <- status_code(r)
access_details <- content(r)
if (status < 200 || status > 299) {
  print(paste("ERROR", "Get request to smtp access failed with status", status, "body", body))
  stop
}
#</gen>

#<gen>r_access_details
# extract smtp authentication details from response
host <- access_details$smtpServerHost
port <- access_details$smtpServerPort
username <- access_details$smtpUsername
password <- access_details$smtpPassword
#</gen>

#<gen>r_get_inbox
# get an inbox to send with
print("Fetching inbox details")
r <- GET("https://api.mailslurp.com/inboxes/paginated?page=0&size=1", add_headers("x-api-key" = api_key))
status <- status_code(r)
inbox_list <- content(r)
if (status < 200 || status > 299) {
  print(paste("ERROR", "Get request to inboxes failed with status", status, "body", body))
  stop
}
#</gen>

#<gen>r_list_email
email_address <- inbox_list$content[[1]]$emailAddress
address = paste("<", email_address, ">", sep="")
print (paste("Found email address:", address))
#</gen>


#<gen>r_compose_email
email <- compose_email(
  body = md(c("Test email"))
)
print (paste("Enter password when prompted", password))
smtp_send(
  email = email,
  from = email_address,
  to = email_address,
  credentials = creds(
    host = host,
    port = port,
    user = username
  )
)
#</gen>

rlang-email-sending-in-r/README.md

# How to send emails in R
The statistical modelling language R can also send emails! This can be very useful to notify an engineer of the results of your script.

## Setup environment
- Install RLang by [downloading it](https://cloud.r-project.org/) or run `brew install r`
- Verify RScript installation `Rscript --version`
- Install sendmailR `Rscript -e 'install.packages("sendmailR",repos="http://cran.r-project.org")'`

## Create a script
- Create a `main.r` file to like that used in this repo.

rlang-email-sending-in-r/Makefile

-include ../.env

install:
	Rscript -e 'install.packages("sendmailR",repos="http://cran.r-project.org")'
	Rscript -e 'install.packages("httr",repos="http://cran.r-project.org")'
	Rscript -e 'install.packages("blastula",repos="http://cran.r-project.org")'

test: smtp smtp_auth

smtp:
	API_KEY=$(API_KEY) Rscript main.r

smtp_auth:
	API_KEY=$(API_KEY) Rscript main-auth.r