如何使用循环从 R 中发送电子邮件
How to send emails from within R using a loop
下面的 R 代码向收件人发送电子邮件,收件人的电子邮件地址是从名为 email
的文本文件中读取的。由于 Gmail 不允许一次发送超过 100 封,我想使用一个循环将电子邮件发送到前 100 封,然后是 101-200,然后是 201-300,依此类推。任何人都可以帮我吗?
library(mailR)
sender <- "xyz@gmail.com"
recipients <- scan("email.txt", encoding="UTF-8",character(0))
send.mail(from = sender,
to = recipients,
subject="Subject of the Mail",
body = "Body of the Mail",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name="convert2latex@gmail.com", passwd="Mypassword", ssl=TRUE),
authenticate = TRUE, send = TRUE)
这是未经测试,但应该为您提供一个跳板供您自己探索。从概念上讲,这就像一个 for
循环,但是 FUN
应用于 INDEX
中每个因子的 recipients
。我正在使用 tapply
,它根据 INDEX
削减 recipients
(您可以自行计算)。您可以插入 Sys.sleep
以防止 "loop" 发送太快。
如果您将 browser()
作为函数的第一行插入(并且 运行 它),您将被放置在函数内部,您可以进一步探索它在做什么。
tapply(X = recipients, INDEX = as.integer(cumsum(rep(1, 252))/100), FUN = function(x, recipients, sender) {
send.mail(from = sender,
to = recipients,
subject="Subject of the Mail",
body = "Body of the Mail",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name="convert2latex@gmail.com", passwd="Mypassword", ssl=TRUE),
authenticate = TRUE, send = TRUE)
}, recipients = recipients, sender = sender)
根据Gmail Sending Limits, you can send to up to 99 addresses with one email. Maybe you want to put those into the BCC field? Here is a test example for Gmail (remember to allow unsecure apps first并指定sender
、user.name
和passwd
:
library(mailR)
sender <- "...@gmail.com" # gmail user
# create 5 test addresses from `sender`
testEmails <- paste(sapply(1:5, function(x) sub("(.*)(@.*)", paste0("\1+test", x, "\2"), sender)), collapse = "\n")
cat(testEmails) # print the addresses
recipients <- scan(file = textConnection(testEmails), encoding="UTF-8",character(0))
## Create a list of vectors of emails with the max size adrPerBatch each
getRecipientBatches <- function(emails, adrPerBatch = 98) {
cuts <- seq(from = 1, to = length(emails), by = adrPerBatch)
recipientBatches <- lapply(cuts, function(x) c(na.omit(emails[x:(x+adrPerBatch-1)])))
return(recipientBatches)
}
## send the 3 test batches à 2/1 address(es)
res <- lapply(getRecipientBatches(recipients, adrPerBatch = 2), function(recipients) {
send.mail(from = sender,
to = sender,
bcc = recipients,
subject="Subject of the Mail",
body = "Body of the Mail",
smtp = list(host.name = "smtp.gmail.com",
port = 465,
user.name = "...",
passwd = "...",
ssl = TRUE),
authenticate = TRUE,
send = TRUE)
})
下面的 R 代码向收件人发送电子邮件,收件人的电子邮件地址是从名为 email
的文本文件中读取的。由于 Gmail 不允许一次发送超过 100 封,我想使用一个循环将电子邮件发送到前 100 封,然后是 101-200,然后是 201-300,依此类推。任何人都可以帮我吗?
library(mailR)
sender <- "xyz@gmail.com"
recipients <- scan("email.txt", encoding="UTF-8",character(0))
send.mail(from = sender,
to = recipients,
subject="Subject of the Mail",
body = "Body of the Mail",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name="convert2latex@gmail.com", passwd="Mypassword", ssl=TRUE),
authenticate = TRUE, send = TRUE)
这是未经测试,但应该为您提供一个跳板供您自己探索。从概念上讲,这就像一个 for
循环,但是 FUN
应用于 INDEX
中每个因子的 recipients
。我正在使用 tapply
,它根据 INDEX
削减 recipients
(您可以自行计算)。您可以插入 Sys.sleep
以防止 "loop" 发送太快。
如果您将 browser()
作为函数的第一行插入(并且 运行 它),您将被放置在函数内部,您可以进一步探索它在做什么。
tapply(X = recipients, INDEX = as.integer(cumsum(rep(1, 252))/100), FUN = function(x, recipients, sender) {
send.mail(from = sender,
to = recipients,
subject="Subject of the Mail",
body = "Body of the Mail",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name="convert2latex@gmail.com", passwd="Mypassword", ssl=TRUE),
authenticate = TRUE, send = TRUE)
}, recipients = recipients, sender = sender)
根据Gmail Sending Limits, you can send to up to 99 addresses with one email. Maybe you want to put those into the BCC field? Here is a test example for Gmail (remember to allow unsecure apps first并指定sender
、user.name
和passwd
:
library(mailR)
sender <- "...@gmail.com" # gmail user
# create 5 test addresses from `sender`
testEmails <- paste(sapply(1:5, function(x) sub("(.*)(@.*)", paste0("\1+test", x, "\2"), sender)), collapse = "\n")
cat(testEmails) # print the addresses
recipients <- scan(file = textConnection(testEmails), encoding="UTF-8",character(0))
## Create a list of vectors of emails with the max size adrPerBatch each
getRecipientBatches <- function(emails, adrPerBatch = 98) {
cuts <- seq(from = 1, to = length(emails), by = adrPerBatch)
recipientBatches <- lapply(cuts, function(x) c(na.omit(emails[x:(x+adrPerBatch-1)])))
return(recipientBatches)
}
## send the 3 test batches à 2/1 address(es)
res <- lapply(getRecipientBatches(recipients, adrPerBatch = 2), function(recipients) {
send.mail(from = sender,
to = sender,
bcc = recipients,
subject="Subject of the Mail",
body = "Body of the Mail",
smtp = list(host.name = "smtp.gmail.com",
port = 465,
user.name = "...",
passwd = "...",
ssl = TRUE),
authenticate = TRUE,
send = TRUE)
})