你如何从R发送电子邮件?

时间:2022-08-07 21:39:07

I want to send emails from R. This is what I have so far:

我想从r发邮件,这是我目前为止所拥有的:

library(sendmailR)


from <- "eamil@example.com"
to <- "email2@example.com"
subject <- "Performance Result"
body <- "This is the result of the test:"                     
mailControl=list(smtpServer="snmpt server address")

sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)

When I execute this script, my R session hangs. Any ideas what might be happening?

当我执行这个脚本时,我的R会话挂起。有什么想法吗?

4 个解决方案

#1


14  

I just tried it out, and it worked for me.

我只是试了一下,它对我很有效。

My only differences were I used <> for the from and to:

我唯一的区别是我用了<> for the from和to:

from = "<email1@dal.ca>"
to = "<email2@gmail.com>"

and my mail control was different, I used

我的邮件控制也不一样。

control=list(smtpServer="ASPMX.L.GOOGLE.COM"))

#2


32  

If you need to be able to use an smtp server with authentication you can use the mailR package.

如果您需要能够使用带有身份验证的smtp服务器,您可以使用mailR包。

For example using gmail's smtp server:

例如,使用gmail的smtp服务器:

library(mailR)
sender <- "SENDER@gmail.com"
recipients <- c("RECIPIENT@gmail.com")
send.mail(from = sender,
          to = recipients,
          subject = "Subject of the email",
          body = "Body of the email",
          smtp = list(host.name = "smtp.gmail.com", port = 465, 
                      user.name = "YOURUSERNAME@gmail.com",            
                      passwd = "YOURPASSWORD", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

#3


2  

Sorry for bumping up this thread. If you want to send email from R using Microsoft outlook, below is the way to go using the RDCOMClient package. I myself spent a lot of time trying to find an answer on this. I thought it would be useful to have this solution too in this thread for users.

对不起,把这根线撞了。如果你想用微软outlook发送电子邮件,下面是使用RDCOMClient包的方法。我花了很多时间试图找到答案。我认为在这个线程中为用户提供这个解决方案是很有用的。

Full credit to @agstudy who provided the original solution in this link - Sending email in R via outlook

完全归功于@agstudy,他提供了这个链接的原始解决方案——通过outlook发送电子邮件。

library (RDCOMClient)

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "Test Subject"
outMail[["body"]] = "Body of email"               
outMail$Send()

#4


1  

library(mailR)
sender <- "abc@gmail.com"

recipients <- c("bcd@gmail.com","xyz@gmail.com")

send.mail(from = sender, to = recipients, subject="Cash_Collected_Bank_transfer",Sys.Date(),"{}", body = Summary1, encoding = "utf-8", 
    smtp = list(host.name = "smtp.gmail.com", port = 465, 
    user.name="abc@gmail.com", passwd="abc@1234", ssl=TRUE), authenticate = TRUE, send = TRUE ,attach.files = c(path2),html = TRUE , inline = TRUE )

#1


14  

I just tried it out, and it worked for me.

我只是试了一下,它对我很有效。

My only differences were I used <> for the from and to:

我唯一的区别是我用了<> for the from和to:

from = "<email1@dal.ca>"
to = "<email2@gmail.com>"

and my mail control was different, I used

我的邮件控制也不一样。

control=list(smtpServer="ASPMX.L.GOOGLE.COM"))

#2


32  

If you need to be able to use an smtp server with authentication you can use the mailR package.

如果您需要能够使用带有身份验证的smtp服务器,您可以使用mailR包。

For example using gmail's smtp server:

例如,使用gmail的smtp服务器:

library(mailR)
sender <- "SENDER@gmail.com"
recipients <- c("RECIPIENT@gmail.com")
send.mail(from = sender,
          to = recipients,
          subject = "Subject of the email",
          body = "Body of the email",
          smtp = list(host.name = "smtp.gmail.com", port = 465, 
                      user.name = "YOURUSERNAME@gmail.com",            
                      passwd = "YOURPASSWORD", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

#3


2  

Sorry for bumping up this thread. If you want to send email from R using Microsoft outlook, below is the way to go using the RDCOMClient package. I myself spent a lot of time trying to find an answer on this. I thought it would be useful to have this solution too in this thread for users.

对不起,把这根线撞了。如果你想用微软outlook发送电子邮件,下面是使用RDCOMClient包的方法。我花了很多时间试图找到答案。我认为在这个线程中为用户提供这个解决方案是很有用的。

Full credit to @agstudy who provided the original solution in this link - Sending email in R via outlook

完全归功于@agstudy,他提供了这个链接的原始解决方案——通过outlook发送电子邮件。

library (RDCOMClient)

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "Test Subject"
outMail[["body"]] = "Body of email"               
outMail$Send()

#4


1  

library(mailR)
sender <- "abc@gmail.com"

recipients <- c("bcd@gmail.com","xyz@gmail.com")

send.mail(from = sender, to = recipients, subject="Cash_Collected_Bank_transfer",Sys.Date(),"{}", body = Summary1, encoding = "utf-8", 
    smtp = list(host.name = "smtp.gmail.com", port = 465, 
    user.name="abc@gmail.com", passwd="abc@1234", ssl=TRUE), authenticate = TRUE, send = TRUE ,attach.files = c(path2),html = TRUE , inline = TRUE )