Analysis in R: Send mail from R! The “emayili” package

RAnalytics

Introducing a package that allows you to send mail from R. It is possible to attach files, embed images in the body of the message, and attach Rmd files. In the execution command, we will show an example of sending mail using Gmail account information. Note that not only Gmail but other mail services can be used as long as the SMTP server information is available.

Even if you have 2-step verification set up for your Google Account, you can still use it by retrieving your password under “Security,” “Sign in to Google,” and “Apps password” in your account settings.

Package version is 0.7.13. Checked with R version 4.2.2.

スポンサーリンク
Sponsored Link

Install Package

Run the following command.

#Install Package
install.packages("emayili")

Example

See the command and package help for details.

#Loading the library
library("emayili")

#Preparing to send mail,create envelope object: envelope command
email <- envelope()

#Set from, to, cc, etc.: from, to, cc, bcc commands
#The from and to commands are used in this example
email <- email %>%
  from("xxxxxx@gmail.com") %>%
  to("xxxxxxxx@gmail.com")

#Set mail title: subject command
email <- email %>%
  subject("TEST:KARADA-GOOD")

#Set mail body: text command
#Embedding is also possible with {{XXXX}}
text <- "karada-good!!"
email <- email %>%
  text("これはテストです!!
       {{text}}")

#Set attachment: attachment command
email <- email %>%
  attachment(paste(as.character(tcltk::tkgetOpenFile(title = "Attachement File"),
                                sep = "", collapse ="")))

#Embedding the output of an Rmd file in the body text: render command
#[https://www.karada-good.net/analyticsr/r-228/]
email <- email %>%
  render(paste(as.character(tcltk::tkgetOpenFile(title = "Select Rmd File"),
                            sep = "", collapse ="")),
         include_css = "rmd")

#Embed images in body text: Combination of html command and attachment command
#The key is to set it with cid
html_body <- '<html><body><img src="cid:image" width="600" height="150"></body></html>'
email <- email %>%
  html(html_body) %>%
  attachment(path = paste(as.character(tcltk::tkgetOpenFile(title = "ファイルを選択"),
                                       sep = "", collapse ="")),
             cid = "image")

#Set up a gmail account with information: gmail command
#from your Google account if you have set up 2-step verification
#Get your login and app password from Google for security
#Set app password instead of login password for password
GmailSend <- gmail(username = "XXXXXX@gmail.com", password = "sbtfbwxxxxxxx")
#Send out an email using the GmailSend function you created
GmailSend(email, verbose = TRUE)

#How to configure when using other services, etc.: server command
smtp <- server(
  host = "XXXXXXXXXXXX",
  port = XXXXXXX,
  username = "XXXXXXXXXX",
  password = "XXXXXXXXXXXXXXX"
)
smtp(envelope object, verbose = TRUE)

Output Example


I hope this makes your analysis a little easier !!

Copied title and URL