Send mail with gmail and R - r

I am simply trying to send an email with R through my gmail account using the mailR package but it does not seem to work.
I get this error:
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
org.apache.commons.mail.EmailException: Sending the email to the following server failed : aspmx.l.google.com:25
Below is the code with anonymized gmail adresses.
install.packages("mailR")
library(mailR)
sender <- "sender#gmail.com" # Replace with a valid address
recipients <- c("receiver1#gmail.com") # Replace with one or more valid addresses
email <- send.mail(from = sender,
to = recipients,
subject="Subject of the email",
body = "Body of the email",
smtp = list(host.name = "aspmx.l.google.com", port = 25),
authenticate = FALSE,
send = FALSE)
email$send() # execute to send email

I finally managed to make it work ! You have to authorize the app on Google. This link really helped me: https://medium.com/airbnb-engineering/using-googlesheets-and-mailr-packages-in-r-to-automate-reporting-c09579e0377f

Related

Can't Authenticate using mailR::send.mail

Code is as follows, The email, password and host name are correct
library(mailR)
library(rJava)
sender <- "sender#email"
recipients <- c("recipient#email")
send.mail(from = sender,
to = recipients,
subject="Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.office365.com", port = 587,
user.name="sender#email", passwd="password", ssl=F,tls = TRUE),
authenticate = T,
send = TRUE,
debug = T)
The debug produces
DEBUG SMTP: protocolConnect login, host=smtp.office365.com, user=sender#email, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
This is to replace some code using RDCOMClient as the package is failing to install.

Sending Outlook Emails via R workaround on OSX

I've looked throughout the site but have not been able to find an answer.
I need to use R to send emails via my works outlook email. It has to be from Outlook, not anywhere else.
Problem is, the computer I'm using is OSX so RDCOMClient won't work.
EDIT: Tried this and it wouldn't work.
sender<-"myemail#outlook.com"
recipients<-c("myemail#outlook.com")
send.mail(from = sender, to = recipients,
subject = "Test",
body = BodyOfMessage,
smtp = list(host.name = "smtp-mail.outlook.com"),
authenticate = FALSE,
html = TRUE,
send = TRUE)Does anyone have a workaround?
And it resulted in this error:
org.apache.commons.mail.EmailException: Sending the email to the
following server failed : smtp-mail.outlook.com:25 at
org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410) at
org.apache.commons.mail.Email.send(Email.java:1437) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
RJavaTools.invokeMethod(RJavaTools.java:386) Caused by:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was
not authenticated to send anonymous mail during MAIL FROM
[BN6PR19CA0117.namprd19.prod.outlook.com]
at
com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2202)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1693)
at
com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1194)
at javax.mail.Transport.send0(Transport.jaNULL va:254) at
javax.mail.Transport.send(Transport.java:124) at
org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400) ... 6
more Error: EmailException (Java): Sending the email to the following
server failed : smtp-mail.outlook.com:25
So, you wouldn't necessarily be sending this through your Outlook client, which is all that Outlook is. You would want to allow the R script you write and the libraries employed to be an email client. I use mailR with a lot of success. Some people like sendmailR for sending messages. They both have their advantages. Your email administrator might allow unauthenticated sending if you run a lot of scripting from a host. Or you can authenticate in your script.
For example:
library(mailR)
#################
# Generate Spam #
#################
BodyOfMessage <- paste("<html><body><p>Hello,</p><p>This is an email message.</p>
<hr>
<p>The second table is a list of users that need to be toggled in the system, by adding them to the correct securitygroup.</p>
<p>", toggle.these.people, "</p>
<p>Scott</p></body></html>")
#mailR
sender<-"fromwho#fromyou.org"
recipients<-c("emailtosendto#email.com")
send.mail(from = sender, to = recipients,
subject = paste("Blah. Created: today.", sep = ""),
body = BodyOfMessage,
smtp = list(host.name = "smtp.exchangeserver.org"),
authenticate = FALSE,
html = TRUE,
attach.files = CSVFileNameIs,
send = TRUE)
This is what I use and it works fine for me.
library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "ryanshuell#gmail.com"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
## send it
outMail$Send()

sending mail from R (mailR)

I am trying to send mail from R on a windows 7 (home) machine. I tried the following code
send.mail(from = "mymailid#gmail.com",
to = c("mymailid#gmail.com"),
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "myuserid", passwd = "my password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
I get the following error:
Error in ls(envir = envir, all.names = private) :
invalid 'envir' argument
after that I tried set up an hmail server (with a local domain name and user account, and smtp set up for smtp.gmail.com:25)
send.mail(from = "localuser#localdomain.local",
to = c("mymailid#gmail.com"),
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "mail.hmailserver.com", port = 25),
authenticate = FALSE,
send = TRUE)
I still get the same error. Any help will be greatly appreciated. Thanks vm
If you're using gmail account, you might need to allow acess to your account from less secure aplications, using this link https://www.google.com/settings/security/lesssecureapps
If you have as well a 2 step verification, you also need to deactivate that.
If your gmail account is propertly setup (as mOnhawk suggested) then this form should work for the smtp list:
smtp = list(host.name = "smtp.gmail.com", port = 465,
ssl=TRUE, user.name = "mymailid#gmail.com",
passwd = "my password)
i am trying to send email using RDCOMClient and here is the code
library(RDCOMClient)
emails <- paste("emailid")
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = emails
outMail[["Cc"]] = "emailid"
outMail[["subject"]] = "Monthly Report for Compliance"
outMail[["body"]] = paste(" Please find the monthly report completed for ",Sys.Date(),
".\n\n instrument file is saved at:\n G:\\Data Science\\Monthly Check - Westlake\\Instruments and Issuers Compliance List
"
)
outMail[["attachments"]]$Add("G:\\Data Science\\Monthly Check - Westlake\\Instruments and Issuers Compliance List\\Instruments_tracked-.02202018.csv")
outMail$Send()
I get the below error
<checkErrorInfo> 80020009
No support for InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
You can try the following example:
library(mail)
to <- "albert.physik#gmail.com"
subject <- "mail of R"
msg <- paste("Testing!")
sendmail(to, subject , msg)
But only allows you to send 20 emails per day
I hope you find it useful.
Luck!
Did you use empty string as the body?
I tried with my company email and also gmail. When I put
body = "", # quotation marks with nothing in between
it yields
Error in ls(envir = envir, all.names = private) : invalid 'envir'
argument
While this works:
body = " ", # adding a space there
Don't know why at all... If you happened to have empty string, this might help.
Subject being empty string causes no problem:
subject = "", # quotation marks with nothing in between

how do you send email from R

I want to send emails from R. This is what I have so far:
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?
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 <- "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)
I just tried it out, and it worked for me.
My only differences were I used <> for the from and to:
from = "<email1#dal.ca>"
to = "<email2#gmail.com>"
and my mail control was different, I used
control=list(smtpServer="ASPMX.L.GOOGLE.COM"))
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 - Sending email in R via 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()
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 )
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("alice#yahoo.com") %>%
to("bob#google.com") %>%
subject("This is a plain text message!") %>%
body("Hello!")
smtp <- server(host = "smtp.gmail.com",
port = 465,
username = "bob#gmail.com",
password = "bd40ef6d4a9413de9c1318a65cbae5d7")
smtp(email, verbose = TRUE)
There are two ways to send an email via Gmail, anonymized or authenticated. Here is the code for anonymized:
library(mailR)
send.mail(from = "sender#gmail.com",
to = c("Recipient 1 <recipient1#gmail.com>", "recipient2#gmail.com"),
cc = c("CC Recipient <cc.recipient#gmail.com>"),
bcc = c("BCC Recipient <bcc.recipient#gmail.com>"),
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.
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.' youremail#anymail.com < /dev/null", intern = TRUE))
You will need to install mutt in terminal before this.
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)

Send Mail R and Socket Connections

I am attempting to use the SendMailR function, I have checked with our I.T. department that I am using the correct server and I have the right permissions to connect and they have sent emails via this server but not through R and I have also checked that the port should be 25.
The code:
# E-Mail #
library(sendmailR)
from <- "david#work.com"
to <- "adam#work.com"
subject <- "Send Mail R- Test"
body <- "TESTING TESTING TESTING"
mailControl=list(smtpServer="uksmtp.global.local")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
I receive the below error:
function (host = "localhost", port, server = FALSE, blocking = FALSE,
open = "a+", encoding = getOption("encoding"), timeout = getOption("timeout"))
.Internal(socketConnection(host, port, server, blocking, open,
encoding, timeout))
<bytecode: 0x00000000071beb18>
<environment: namespace:base>
So I figured its an error with or I needed to define a new socket connection, is this where the problem lies? Could anyone give me any pointers on where to go next with this to get it working?
Thanks in advance
Although this is not an answer to your question you might want to alternatively check out the mail package which offers an alternative way to sent mail via an external mail server and address:
require(mail)
sendmail("adam#work.com", "Send Mail R- Test", "TESTING TESTING TESTING")
Note that you can only sent 20 mails that way per day (to avoid spam).
You could give the new mailR package a shot that allows SMTP authorization: http://cran.r-project.org/web/packages/mailR
The following call should then work:
send.mail(from = "sender#gmail.com",
to = "recipient#gmail.com",
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "recipient", passwd = "PASSWORD", ssl = TRUE, tls = TRUE),
authenticate = TRUE,
send = TRUE)
Just replace the key-value pairs in the parameter smtp and you should be set to go.

Resources