gpt4 book ai didi

email - 如何使用 R 发送/接收(SMTP/POP3)电子邮件?

转载 作者:行者123 更新时间:2023-12-04 00:40:29 30 4
gpt4 key购买 nike

我强烈怀疑最受好评的答案将是“这是工作的错误工具”。我承认 R 可能不是特别适合发送和接收电子邮件,但它是我最了解的脚本语言。我希望找到一种在 R 中发送和接收短电子邮件的方法。有人知道在 Windows 平台上执行此操作的既定方法吗?我也许可以结合使用 BLAT 和 GetMail,但首选 native R 解决方案。

编辑:可接受的解决方案应该能够与需要 SSL 的服务器连接。

编辑 2:我提供了 80% 的答案。遗憾的是,没有演示 R native 方式。相反,我使用了可能无法跨平台兼容的系统调用和命令行程序的邪恶组合。 R native 调用需要深入研究 POP3 服务器喜欢与连接的客户端交谈的方式,以及我目前没有的对 SSL 的理解。其他答案仍然受到鼓励。

##Note: Other programs are wrapped in R functions and system calls.
#They each have their own licenses which may or may not allow the use suggested here
#Programs used here:
#STunnel: http://www.stunnel.org/; Provides an SSL tunnel but requires OpenSSL
#OpenSSL: http://www.openssl.org/; OpenSSL to actually provide SSL
# Note that these .dlls should be placed with the stunnel exe.
# Also note that libssl32.dll may need to be renamed from ssleay32.dll
#Microsoft Visual C++ 2008 Redistributable (may be required for the SSL .dlls to work correctly)
#Blat: http://www.blat.net; a public domain SMTP sending program
#Getmail is free for non-commercial use. If you use it in a business environment, then a fee of $50 USD is payable to Tim Charron.

#Stunnel is a TSR, so it will need to be killed from the task manager if there is an issue. If you are willing to install it as a service you may be able to tweak my code to start and stop the service.
#My current code does not create .conf file for stunnel the way a full version ought. Check http://spampal.sanesecurity.com/manual_eng/servers/stunnel/stunnel.htm#sconfig21 to create the appropriate configuration file.

#Set the config values as appropriate
##Config##
BLAT.loc <- "c:/Programming/R/Rmail/blat262/full/blat.exe"
GetMail.loc <- "C:/Programming/R/RMail/getmail133/getmail.exe"
stunnel.loc <- "C:/Programming/R/RMail/stunnel/stunnel-4.11.exe"

#The set mail function assigns the username and password to be used as well as the smtp and pop3 servers it starts stunnel (and assumes that the stunnel.conf file is present and set correctly).
setMail <- function(user,pw,SSL=FALSE,smtp="127.0.0.1:259",pop3="127.0.0.1:1109")
{
if (SSL==TRUE)
{
print("Starting stunnel; you will need to kill this from the task-manager")
system(stunnel.loc,wait=FALSE)
Sys.sleep(2) #Give it time to start
}
return(list(user=user,pw=pw,smtp=smtp,pop3=pop3,SSL=SSL))
}

#function to send mail, myMail is the resulting list from setMail
sendmail <- function(myMail, to, subject, msg,VERBOSE=FALSE)
{
writeLines(msg, "out.txt", sep = "\n", useBytes = FALSE)
targ <- paste(getwd(),"/out.txt",sep="")
call <- paste(BLAT.loc, ' "',targ,'" -subject "',subject,'" -to ',to," -u ",myMail$user," -pw ",myMail$pw, " -f ",myMail$user, " -debug -server ",myMail$smtp,sep="")
res <- system(call,intern=TRUE)
if (VERBOSE) {return(res)}
}

#function to get mail, myMail is the resulting list from setMail; it returns a list with one element that contains everything unparsed, another list provides the number of messages remaining on the server.
getmail <- function(myMail,VERBOSE=FALSE)
{
unlink("MSG1.txt") #drop previous get
#download next message
call <- paste(GetMail.loc," -u ",myMail$user," -pw ",myMail$pw," -s ",strsplit(myMail$pop3,":")[[1]][1],
" -port ",strsplit(myMail$pop3,":")[[1]][2]," -n 1",sep="")
res <- system(call,intern=TRUE)
if (VERBOSE) {print(res)}
nmsgtxt <- res[grep("messages on the server.",res)]
nstart <- regexpr("There are",nmsgtxt)
nend <- regexpr("messages on the server.",nmsgtxt)
nmess <- as.numeric(substr(nmsgtxt,10,nend-1))-1
x <- readLines("MSG1.txt",-1)
return(list(message=x,remaining=nmess))
}

使用案例 :简单地说,我需要让 R 能够将其内容在 R 脚本中的其他地方确定的消息发送到 SMTP 服务器。参与者将收到电子邮件并回复。我需要从我的 POP3 服务器检索他们的响应并将其存储在 R 数据结构中,以便我可以对其进行后期处理。在实践中,我正在建立一种通过 R 进行体验抽样的方法。也就是说,R 可以通过电子邮件发送给参与者“你今天过得怎么样(1 = 糟糕;7 = 很棒)?”参与者可以回答“4”,我可以将提出的问题、回答等匹配到数据库中进行统计分析。

最佳答案

从 Pop 服务器拉取消息

为了尝试实现@JorisMeys 利用其他语言的想法,我尝试使用 Python 和 rJython 包从 Gmail(通过 ssl)提取邮件。 Jython 是在 Java 虚拟机上实现的 Python,所以使用 rJython 对我来说有点像使用 R 调用 Java,然后伪装成 Python。

我发现 rJython 对于简单的事情很容易,但是由于我不精通 S4 对象和 (r)Java,我有时很难正确操作来自 rJython 的返回对象。但是,它有效。这是一个从 Gmail 帐户中提取单个邮件的基本结构:

library(rJython)

rJython <- rJython( modules = "poplib")

rJython$exec("import poplib")
rJython$exec("M = poplib.POP3_SSL('pop.gmail.com', 995)")
rJython$exec("M.user(\'yourGmailAddy@gmail.com\')")
rJython$exec("M.pass_(\'yourGmailPassword\')")
rJython$exec("numMessages = len(M.list()[1])")
numMessages <- rJython$get("numMessages")$getValue()

# grab message number one. Loop here if you
# want more messages
rJython$exec("msg = M.retr(1)[1]")
emailContent <- rJython$get("msg")

# turn the message into a list
contentList <- as.list(emailContent)
# so we have an R list... of Java objects
# To get a more native R list we have to
# yank the string from each Java item

messageToList <- function(contentList){
outList <- list()
for (i in 1:length(contentList)){
outList[i] <- contentList[[i]]$toString()
}
outList
}

messageAsList <- messageToList(contentList)
messageAsList

关于email - 如何使用 R 发送/接收(SMTP/POP3)电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4241812/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com