Analysis in R: Sending Gmail from R. The ‘mailR’ package

RAnalytics
スポンサーリンク

The most annoying thing is when you send an e-mail with CC instead of BCC. Have you ever thought that if the CC setting field was eliminated, there would be no trouble? The method of sending e-mail with R introduced in this issue allows you to disable the CC setting, which I believe will reduce the risk of trouble.

The code we will present uses Gmail, but it is possible to send email using SMTP server settings other than Gmail.

This package can be used even if you have 2-tier authentication with Gmail. You can send HTML emails as well as text emails. Of course, file attachments are also possible.

Package version is 0..8. Checked with R version 4.2.2.

スポンサーリンク

Install Java

Depending on your computer environment, you may need to install Java. Please download Java from the following link according to your OS environment (64bit/32bit).

Download java:https://www.java.com/ja/download/manual.jsp

Some packages do not require Java to be installed. Why not use the package that best suits your needs?

Install Package

Run the following command.

install.packages("mailR")

If you use 2-step authentication in Gmail

  1. Go to Gmail, click the account name in the upper right corner, and click the account in the dialog that appears.
  2. Click “App Password” that appears under “Login.
  3. Select “Other” from “Select Device” and “Select App” displayed at the bottom and give it an appropriate name.
  4. Once set, the password is displayed. Paste it into text, etc.

Excel file of email transmission settings

Please prepare an Excel file in the format shown in the image. Enter your email address in the To field. One address per cell, entered vertically.

Load the required packages, load the Excel file with the email sending settings, and set the sender address.

Please check the comments for details.

#Loading the library
library("mailR")
library("XLConnect")
library("tcltk")

###Loading an excel file of email sending settings#####
#Specify Excel file
LoadData <- loadWorkbook(paste(as.character(tkgetOpenFile(title = "Select EXCEL",
                                                          filetypes = '{"xlsx" {".xlsx"}}',
                                                          initialfile = "*.xlsx")), sep = "", collapse =" "))
#sheet1
SendData <- readWorksheet(LoadData, sheet = 1)
########

#Set the sender's address
SendFrom <- "Enter Mail address"
########

Commands for sending e-mail

You may want to comment out CC to prevent errors when setting BCC.
Type “Gmail username” in user.name and “Gmail password” in passwd. If you are using 2-step authentication in Gmail, see the explanation above for the password.

Sending Text email

send.mail(from = SendFrom,
          to = SendData[, 1][!is.na(SendData[, 1])],
          cc = SendData[, 2][!is.na(SendData[, 2])],
          bcc = SendData[, 3][!is.na(SendData[, 3])],
          subject = SendData[, 4][!is.na(SendData[, 4])],
          body = SendData[, 5][!is.na(SendData[, 5])],
          encoding = "utf-8",
          smtp = list(host.name = "smtp.gmail.com", port = 465,
                      user.name = "Gmail username",
                      passwd = "Gmail password", ssl = T),
          authenticate = TRUE,
          send = TRUE)

Sending HTML email

send.mail(from = SendFrom,
          to = SendData[, 1][!is.na(SendData[, 1])],
          cc = SendData[, 2][!is.na(SendData[, 2])],
          bcc = SendData[, 3][!is.na(SendData[, 3])],
          subject = SendData[, 4][!is.na(SendData[, 4])],
          body = paste(as.character(SendData[, 6][!is.na(SendData[, 6])]), sep = "", collapse =" "),
          html = TRUE,
          inline = TRUE,
          encoding = "utf-8",
          smtp = list(host.name = "smtp.gmail.com", port = 465,
                      user.name = "Gmail username",
                      passwd = "Gmail password", ssl = T),
          authenticate = TRUE,
          send = TRUE)

I hope this makes your analysis a little easier !!

Copied title and URL