how do you send email from R

R

R Problem Overview


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

library(sendmailR)


from <- "[email protected]"
to <- "[email protected]"
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 Solutions


Solution 1 - R

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

For example using gmail's smtp server:

library(mailR)
sender <- "[email protected]"
recipients <- c("[email protected]")
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 = "[email protected]",            
                      passwd = "YOURPASSWORD", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

Solution 2 - R

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

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

from = "<[email protected]>"
to = "<[email protected]>"

and my mail control was different, I used

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

Solution 3 - R

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.

Full credit to @agstudy who provided the original solution in this link - https://stackoverflow.com/questions/26811679/sending-email-in-r-via-outlook

library (RDCOMClient)

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

Solution 4 - R

library(mailR)
sender <- "[email protected]"

recipients <- c("[email protected]","[email protected]")

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="[email protected]", passwd="abc@1234", ssl=TRUE), 
     authenticate = TRUE, send = TRUE ,
     attach.files = c(path2), html = TRUE , inline = TRUE )

Solution 5 - R

There is a new package called emayili with two very interesting promises:

  • works on all manner of SMTP servers
  • has minimal dependencies (or dependencies which are easily satisfied)

It seems early stages but promising nonetheless. Sending email is as simple as:

devtools::install_github("datawookie/emayili")
library(emayili)
library(dplyr)

email <- envelope() %>%
  from("[email protected]") %>%
  to("[email protected]") %>%
  subject("This is a plain text message!") %>%
  body("Hello!")

smtp <- server(host = "smtp.gmail.com",
               port = 465,
               username = "[email protected]",
               password = "bd40ef6d4a9413de9c1318a65cbae5d7")

smtp(email, verbose = TRUE)

Solution 6 - R

There are two ways to send an email via Gmail, anonymized or authenticated. Here is the code for anonymized:

library(mailR)
send.mail(from = "[email protected]",
      to = c("Recipient 1 <[email protected]>", "[email protected]"),
      cc = c("CC Recipient <[email protected]>"),
      bcc = c("BCC Recipient <[email protected]>"),
      subject = "Subject of the email",
      body = "Body of the email",
      smtp = list(host.name = "aspmx.l.google.com", port = 25),
      authenticate = FALSE,
      send = TRUE)

Make sure the recipient emails are Gmail too. It most likely goes to the spam folder in the Gmail account so make sure to mark it "not spammed".

You can find more info here.

Solution 7 - R

I found the simplest way in Ubuntu is to run the one liner Terminal command in R. No need for password.

> try(system("mutt -s 'Run is complete.' [email protected] < /dev/null", intern = TRUE))

You will need to install mutt in terminal before this.

Solution 8 - R

If you prefer an in-house solution with your server, you can call the linux sendmail.

EMAIL <- myEmail@gmail.com
cmd <- 'subject="Info server";body="This is an email"'
cmd <- paste("echo -e \"Subject:${subject}\n${body}\" | /usr/sbin/sendmail -t \"", EMAIL, "\"")
system(cmd)      

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1471980View Question on Stackoverflow
Solution 1 - Ralko989View Answer on Stackoverflow
Solution 2 - RSarahView Answer on Stackoverflow
Solution 3 - RCode_SipraView Answer on Stackoverflow
Solution 4 - RPiyush SharmaView Answer on Stackoverflow
Solution 5 - RGorkaView Answer on Stackoverflow
Solution 6 - RHabib KarbasianView Answer on Stackoverflow
Solution 7 - RentropyView Answer on Stackoverflow
Solution 8 - RXavier PrudentView Answer on Stackoverflow