如何在windows中从R发送带有附件的电子邮件
我有一个从windows计算机运行的R脚本 在它完成后,我希望这个脚本自动发送一些日志文件附加电子邮件 将如何在windows中从R发送带有附件的电子邮件,r,email,sendmailr,R,Email,Sendmailr,我有一个从windows计算机运行的R脚本 在它完成后,我希望这个脚本自动发送一些日志文件附加电子邮件 将shell()与其他脚本一起使用可能是可行的,但我想知道在R中是否有更好的解决方案。 谢谢。您查看了sendmailR软件包了吗?它允许SMTP提交邮件,您可能可以编辑该函数以允许附件。再说一次,如果它只有一个日志文件,那么正如您所提到的,它可能值得使用shell()。您愿意接受twitter消息吗?您可以使用Rcurl将更新发布到twitter,然后可以将其作为文本转发到您的手机,或通过通
shell()
与其他脚本一起使用可能是可行的,但我想知道在R中是否有更好的解决方案。
谢谢。您查看了
sendmailR
软件包了吗?它允许SMTP提交邮件,您可能可以编辑该函数以允许附件。再说一次,如果它只有一个日志文件,那么正如您所提到的,它可能值得使用shell()
。您愿意接受twitter消息吗?您可以使用Rcurl将更新发布到twitter,然后可以将其作为文本转发到您的手机,或通过通知设置转发到您的电子邮件
请参见此处:Sendmairr在Windows 7上为我工作。我提到 smtpServer=Outlook 2010的信息位于文件->帐户设置->帐户设置->双击您的帐户->文本“服务器”框中
库(sendmailR)
#设置工作目录
setwd(“C:/workingdirectorypath”)
#####发送普通电子邮件
从Windows的中,可以一起解析VB脚本(参见示例),然后通过shell调用它
这可能是这样的:
SendMail <- function(from="me@my-server.de",to="me@my-server.de",text="Hallo",subject="Sag Hallo",smtp="smtp.my.server.de",user="me.myself.and.i",pw="123"){
require(stringr)
part1 <- "Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM "
part2 <- paste(paste("Set objMessage = CreateObject(",'"',"CDO.Message",'"',")" ,sep=""),
paste("objMessage.Subject = ",'"',subject,'"',sep=""),
paste("objMessage.From = ",'"',from,'"',sep=""),
paste("objMessage.To = ",'"',to,'"',sep=""),
paste("objMessage.TextBody = ",'"',text,'"',sep=""),
sep="\n")
part3 <- paste(
"'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/sendusing\") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/smtpserver\") = ",'"',smtp,'"',"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate\") = cdoBasic
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/sendusername\") = ",'"',user,'"',"
'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/sendpassword\") = ",'"',pw,'"', "
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/smtpserverport\") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/smtpusessl\") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
(\"http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout\") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
",sep="")
vbsscript <- paste(part1,part2,part3,sep="\n\n\n")
str_split(vbsscript,"\n")
writeLines(vbsscript, "sendmail.vbs")
shell("sendmail.vbs")
unlink("sendmail.vbs")
}
SendMail只是想提醒那些想拥有twilio服务自我通知功能的人,他们提供免费服务,可以向自己的手机发送短信。此处提供了使用R的漫游
附加了一个示例代码,只需将凭据替换为您自己的凭据即可
library(jsonlite)
library(XML)
library(httr)
library(rjson)
library(RCurl)
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
authenticate_twilio <- "https://[ACCOUNT SID]:[AUTH TOKEN]@api.twilio.com/2010-04-01/Accounts"
authenticate_response <- getURL(authenticate_twilio)
print(authenticate_response)
postForm("https://[ACCOUNT SID]:[AUTH TOKEN]@api.twilio.com/2010-04-01/Accounts/[ACCOUNT SID]/Messages.XML",.params = c(From = "+1[twilio phone#]", To = "+1[self phone#]",Body = "Hello from twilio"))
library(jsonlite)
库(XML)
图书馆(httr)
图书馆(rjson)
图书馆(RCurl)
选项(rcurlpoptions=list(cainfo=system.file(“curlsl”、“cacert.pem”、package=“RCurl”))
authenticate_twilio使用-它与身份验证、附件一起工作,它会自动发送txt消息以及html等
mailR
需要rJava,有时会有点痛。在windows上,我没有遇到任何问题。在ubuntu上,这解决了我遇到的一个问题:
sudo apt-get install openjdk-jdk
在R
(如果rJava有问题,请在终端中尝试sudo R CMD javareconf
)
mailR
易于使用,并在github页面上有很好的文档记录
文档中的示例
library(mailR)
send.mail(from = "sender@gmail.com",
to = c("recipient1@gmail.com", "recipient2@gmail.com"),
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE,
attach.files = c("./download.log", "upload.log", "https://dl.dropboxusercontent.com/u/5031586/How%20to%20use%20the%20Public%20folder.rtf"),
file.names = c("Download log.log", "Upload log.log", "DropBox File.rtf"), # optional parameter
file.descriptions = c("Description for download log", "Description for upload log", "DropBox File"), # optional parameter
debug = TRUE)
注意:您的smtp服务器可能会发现过度使用可疑。gmail就是这样。因此,在发送了几封邮件之后,您可能必须登录到,并查看该帐户是否已被临时禁用。另外请注意,如果您使用具有双因素身份验证的gmail帐户,则需要使用。到此为止,但您可以通过正确使用RDCOMClient避免使用vbscript,如从R-Help
> sendEmail(ema = "r-help at r-project.org",
name = "R-help mailing list",
subject = "How to send Email from R using the RDCOMClient"
msgBody = "here is the body of the message")
The package RDCOMClient is available at http://www.omegahat.org/RDCOMClient.
"sendEmail" <-
function(ema, name, subject, msgBody, deliverNow = TRUE)
{
require(RDCOMClient)
ema <- paste("SMPT:", ema, sep="") ## prepend protocol to address
## create an e-mail session
session <- COMCreate("Mapi.Session")
session$Logon()
## add a message to the outbox collection of messages
outbox <- session[["Outbox"]]
msg <- outbox[["Messages"]]$Add(subject, msgBody)
## add recipient's name (TODO: addMultiple() or loop, if many recipients)
msg[["Recipients"]]$Add(name, ema)
msg$Send()
if(deliverNow)
msg$DeliverNow()
session$Logoff() ## wrap up
}
发送电子邮件(ema=“r-help at r-project.org”,
name=“R-help邮件列表”,
主题=“如何使用RDCOMClient从R发送电子邮件”
msgBody=“这是消息的正文”)
RDCOMClient包可在以下位置获得:http://www.omegahat.org/RDCOMClient.
“sendEmail”这里是一个使用“mailR”包发送电子邮件的简单代码段
库(mailR)
# 1. 发件人
发件人我猜您在编写shell()
时是指system()
。您好,阿哈拉,我希望Stedy solution会有所帮助。我可以问一下,你用什么来安排R脚本?Thank.Tal,在Windows中使用计划任务(我认为条目类似于R CMD BATCH script.R),cron on on*NIX系统。有一个名为twitteR的CRAN包,用于从R连接twitteR:你能在“电子邮件正文”中使用html吗?你能使用utf-8编码(多语言文本)吗?我不知道。试试看。你是如何为gmail配置serverinfo部分的?我认为Sendmairr不接受身份验证(用户/密码)-我使用mailR就是为了这个原因。@Andreas你的说法似乎是正确的,非常感谢,这就像做梦一样!这是我发现的在R(在Windows上)中工作的唯一方法。正如Paul在其网页中所解释的,如果您使用gmail,则需要将服务器端口设置为465,并将“使用SSL”选项设置为True。我使用以下代码来调用它:SendMail(from=“dag。hjermann@gmail.com“,to=”dhj@niva.no,text=“Hallo”,subject=“Sag Hallo”,smtp=“smtp.gmail.com”,user=“dag。hjermann@gmail.com“,pw=“*******”
插入您的真实密码当然。。。另外,R的工作目录必须在硬盘上的某个地方(C:),这太棒了!是否可以添加附件?我有非常有限的VB经验…所以,这就是我为什么要问的原因。爱帮助:-)。。。jip,这似乎是可能的,尽管我自己从未尝试过,请看这里:标题下的“发送带有附件的文本电子邮件”。。。可以在函数中包含相关的行,也可以从文档中包含一个示例来演示代码的样子。其他答案成功地包含了示例。@Andreas关于附件,我尝试使用fileInput函数,允许用户选择文件而不是硬编码文件名……但它在文件中返回一个错误错误。存在:无效的'file'参数
…有什么想法吗?
install.packages("devtools", dep = T)
library(devtools)
install_github("rpremraj/mailR")
library(mailR)
send.mail(from = "sender@gmail.com",
to = c("recipient1@gmail.com", "recipient2@gmail.com"),
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE,
attach.files = c("./download.log", "upload.log", "https://dl.dropboxusercontent.com/u/5031586/How%20to%20use%20the%20Public%20folder.rtf"),
file.names = c("Download log.log", "Upload log.log", "DropBox File.rtf"), # optional parameter
file.descriptions = c("Description for download log", "Description for upload log", "DropBox File"), # optional parameter
debug = TRUE)
> sendEmail(ema = "r-help at r-project.org",
name = "R-help mailing list",
subject = "How to send Email from R using the RDCOMClient"
msgBody = "here is the body of the message")
The package RDCOMClient is available at http://www.omegahat.org/RDCOMClient.
"sendEmail" <-
function(ema, name, subject, msgBody, deliverNow = TRUE)
{
require(RDCOMClient)
ema <- paste("SMPT:", ema, sep="") ## prepend protocol to address
## create an e-mail session
session <- COMCreate("Mapi.Session")
session$Logon()
## add a message to the outbox collection of messages
outbox <- session[["Outbox"]]
msg <- outbox[["Messages"]]$Add(subject, msgBody)
## add recipient's name (TODO: addMultiple() or loop, if many recipients)
msg[["Recipients"]]$Add(name, ema)
msg$Send()
if(deliverNow)
msg$DeliverNow()
session$Logoff() ## wrap up
}
library(mailR)
# 1. Sender
sender <- "x@me.com"
# 2. Recipients
recipients <- c("y@you.com", "z@our.com")
# 3. Attached files
list_files_attached_location <- c("mtcars.csv")
list_files_attached_names <- c("mtcars name")
list_files_attached_description <- c("mtcars desc")
# 4. Send email
tryCatch({
send.mail(from = sender,
to = recipients,
subject = "Your subject",
body = "Your mail body",
smtp = list(
host.name = "smtp.gmail.com",
port = 465,
user.name = sender,
passwd = "psw",
ssl = TRUE),
authenticate = TRUE,
send = TRUE,
attach.files = list_files_attached_location,
file.names = list_files_attached_names,
file.descriptions = list_files_attached_description,
debug = TRUE
)
},
error = function(e) {
print(e)
stop()
})