Reputation: 31
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<-"[email protected]"
recipients<-c("[email protected]")
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
Upvotes: 3
Views: 1522
Reputation: 440
For the record (because I could not find a current solution): sending emails from R through Outlook is possible with Applescript. For that, Outlook currently needs to be reverted to the legacy version, though the New Outlook is supposed to support it soon.
Then you need an Applescript function saved in a file. This version accepts multiple To recipients - it could obviously be extended for CC, attachments, etc.
on run argv
set varSubject to item 1 of argv
set varContent to item 2 of argv
set shouldSend to item 3 of argv
set recipientDetails to items 4 through end of argv
tell application "Microsoft Outlook"
set newMail to make new outgoing message with properties {subject:varSubject, content:varContent}
repeat with i from 1 to (count recipientDetails) by 2
set varName to item i of recipientDetails
set varEmail to item (i + 1) of recipientDetails
make new recipient at newMail with properties {email address:{name:varName, address:varEmail}}
end repeat
if shouldSend is "true" then
tell newMail to send
end if
end tell
end run
Then this can be called from R like this
send_email <- function(subject, content, names, emails, send = FALSE) {
script_path <- "applescript_send_email.scpt" # Update with the correct path to your script
send_flag <- ifelse(send, "true", "false")
# Combine names and emails
recipient_args <- mapply(function(name, email) c(shQuote(name), shQuote(email)), names, emails, SIMPLIFY = FALSE)
recipient_args <- unlist(recipient_args)
args <- c(shQuote(subject), shQuote(content), send_flag, recipient_args)
system2("osascript", args = c(script_path, args), wait = FALSE)
}
send_email("Hello again", "<b>Good</b>Bye", c("Jon Snow", "Arya Stark"), c("[email protected]", "[email protected]"), TRUE)
Upvotes: 1
Reputation: 20362
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"]] = "[email protected]"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
## send it
outMail$Send()
Upvotes: -1
Reputation: 324
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<-"[email protected]"
recipients<-c("[email protected]")
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)
Upvotes: 1