Sending Outlook Emails via R workaround on OSX - r

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()

Related

Send mail with gmail and 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

Sending an attachment from Shiny

I have created an application using Shiny and uploaded to the server that belongs to shinyapps.io; I have tested it and all is fine. My application creates via the server.R a text file that I would like to send to my email when a user finishes performing a task. I want to send that file to my email because I do not see a way in which to see the files that my shiny application outputs in the shinyapps.io admin tool. So bottomline, how can I send a file from a shiny application to my email?
For example if I have the following:
library(sendmailR)
datos<-read.table("data.txt")
to <- "<loretta#gmail.com>"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="ASPMX.L.GOOGLE.COM")
sendmail(from="localhost",to=to,subject=subject,msg=body,control=mailControl)
attachmentObject <- mime_part(name=datos)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from="localhost",to=to,subject=subject,msg=bodyWithAttachment,control=mailControl)
I would like to know what I should put in the from part, I mean I have put it localhost, but I need to put the address of where the shiny application is running; from where I can get that?
Also when I run the above code, not in a Shiny environment, but as a script I got the following error after the sendmail part:
Error in wait_for(code) :
SMTP Error: 5.5.2 Syntax error. g22si4860678yhc.87 - gsmtp
any help would be great
This works for me for sending email from outlook using the mailR library:
library(mailR)
bodyMsg <- "Some message to be included in the body of the email"
send.mail(from = "sender#xyz.com", to = "receiver#xyz.com", subject = "Some topic",
body = bodyMsg , authenticate = TRUE, html = TRUE, send = TRUE, attach.files = file.path(folder, fileName),
smtp = list(host.name = "abcdef.xyz.com", port = 587, user.name = "sender#xyz.com", passwd = "password", tls = TRUE))}

Send email in R with attachment [duplicate]

I have a scheduled an R script running from a windows machine.
After it finishes, I wish this script to automatically send an email with some log file attached.
Using shell() with some other scripts may be possible, but I was wondering if there is a better solution within R.
Thanks.
sendmailR works for me on Windows 7. I referenced http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf
smtpServer= info for Outlook 2010 is in File -> Account Settings -> Account Settings -> double click your account -> text in "Server" box
library(sendmailR)
#set working directory
setwd("C:/workingdirectorypath")
#####send plain email
from <- "you#account.com"
to <- "recipient#account.com"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="serverinfo")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
#####send same email with attachment
#needs full path if not in working directory
attachmentPath <- "subfolder/log.txt"
#same as attachmentPath if using working directory
attachmentName <- "log.txt"
#key part for attachments, put the body and the mime_part in a list for msg
attachmentObject <- mime_part(x=attachmentPath,name=attachmentName)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from=from,to=to,subject=subject,msg=bodyWithAttachment,control=mailControl)
In addition, multiple files can be sent by adding another mime_part to the msg list as follows (I also condensed it):
attachmentObject <- mime_part(x="subfolder/log.txt",name="log.txt")
attachmentObject2 <- mime_part(x="subfolder/log2.txt",name="log2.txt")
bodyWithAttachment <- list(body,attachmentObject,attachmentObject2)
Use mailR - it works with authentication, attachments, it automatically send txt message along with html and more.
mailR requires rJava which can be a bit of a pain sometimes. On windows I haven't had any problems. On ubuntu this solved the one issue I've had:
sudo apt-get install openjdk-jdk
in R
install.packages("devtools", dep = T)
library(devtools)
install_github("rpremraj/mailR")
(if you have trouble with rJava - try sudo R CMD javareconf in terminal)
mailR is easy to work with and well documented on the github page.
Example from the documentaion
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)
Note: your smtp server might find excessive use suspicious. This is the case with e.g. gmail. So after sending a few mails you probably have to log in to the gmail account and see if the account has been temporarily disabled. Also note that if you use a gmail account with two-factor authentication you need to use an application specific password.
Would you settle for a twitter message? You could use Rcurl to post an update to twitter, which can then be forwarded to your cell phone as a text, or to your email via the notification settings.
See here: http://www.sakana.fr/blog/2007/03/18/scripting-twitter-with-curl/
Have you looked into the sendmailR package yet? It allows SMTP to submit a message and you could probably edit the function to allow an attachment. Then again, if its only one log file it might just be worth it to use shell() as you mentioned.
For Windows one might parse together a VB-Script (see e.g. http://www.paulsadowski.com/wsh/cdo.htm ) and then call it via shell.
This might look like this:
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")
}
Just want to remind people who wants self-notifying feature of a service called twilio, they provide free service to send sms to your own cellphone. A walk-through using R is available here https://dreamtolearn.com/ryan/data_analytics_viz/78
An example code is attached, just replace the credentials with the ones of your own.
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"))
Here is a simple code snippet for sending an e-mail by using "mailR" package
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()
})
For those who suggested using 'mailR' - this is the way to go, but it is difficult to install of ubuntu. Here is a resource to install 'javaR' on ubuntu which will allow you to install 'mailR'
https://www.r-bloggers.com/2018/02/installing-rjava-on-ubuntu/
Late to this, but you can avoid shelling out to vbscript by using RDCOMClient correctly, as in this example from 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
}

How to configure sendmailR to issue STARTTLS

I'm trying to send emails from R using sendmailR package using the following code, which unfortunately fails :
## Set mail contents
from <- sprintf('<sendmailR#%s>', Sys.info()[4])
to <- '<slackline#gmail.com>'
subject <- 'Feeding Plots'
body <- list('Latest feeding graph', mime_part(feeding.plot,
name = "feeding"))
## Set control parameters
control <- sendmail_options(verboseShow = TRUE,
smtpServer ="smtp.gmail.com",
smtpPort = 587,
smtpSTARTTLS = '',
blocking = FALSE)
sendmail(from,
to,
subject,
msg = body,
control = control,
headers)
<< 220 mx.google.com ESMTP xt1sm884721wjb.17 - gsmtp
>> HELO kimura
<< 250 mx.google.com at your service
>> MAIL FROM: <sendmailR#kimura>
<< 530 5.7.0 Must issue a STARTTLS command first. xt1sm884721wjb.17 - gsmtp
Error in wait_for(code) :
SMTP Error: 5.7.0 Must issue a STARTTLS command first. xt1sm884721wjb.17 - gsmtp
The sendmailR manual doesn't mention how to configures STARTTLS although it does indicate that additional arguments can be passed, which is why I have included the option smtpSTARTLS = '' based on whats mentioned in some other threads (here and here). I've tried playing with the argument for smtpSTARTTLS and setting it to TRUE but no joy.
Any pointers to documentation or solutions would be most welcome.
Thanks
As far as I understand it, sendmailR doesn't support any type of login to the SMTP server, hence, gmail is basically unusable. You can only use the package if you are within the right network and set up a server that is only reachable within the network I guess (i.e., one NOT using authentication).
The alternative is the mail package (in which you cannot use your own address).
The reference from the sendmailR documentation is:
SMTP AUTH is currently unsupported.
You could give the new mailR package a shot that allows SMTP authorization: http://cran.r-project.org/web/packages/mailR/index.html
The following call should then work:
send.mail(from = "slackline#gmail.com",
to = "slackline#gmail.com",
subject = "Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "slackline", passwd = "PASSWORD", ssl = TRUE),
authenticate = TRUE,
send = TRUE)

How to send an email with attachment from R in windows

I have a scheduled an R script running from a windows machine.
After it finishes, I wish this script to automatically send an email with some log file attached.
Using shell() with some other scripts may be possible, but I was wondering if there is a better solution within R.
Thanks.
sendmailR works for me on Windows 7. I referenced http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf
smtpServer= info for Outlook 2010 is in File -> Account Settings -> Account Settings -> double click your account -> text in "Server" box
library(sendmailR)
#set working directory
setwd("C:/workingdirectorypath")
#####send plain email
from <- "you#account.com"
to <- "recipient#account.com"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="serverinfo")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
#####send same email with attachment
#needs full path if not in working directory
attachmentPath <- "subfolder/log.txt"
#same as attachmentPath if using working directory
attachmentName <- "log.txt"
#key part for attachments, put the body and the mime_part in a list for msg
attachmentObject <- mime_part(x=attachmentPath,name=attachmentName)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from=from,to=to,subject=subject,msg=bodyWithAttachment,control=mailControl)
In addition, multiple files can be sent by adding another mime_part to the msg list as follows (I also condensed it):
attachmentObject <- mime_part(x="subfolder/log.txt",name="log.txt")
attachmentObject2 <- mime_part(x="subfolder/log2.txt",name="log2.txt")
bodyWithAttachment <- list(body,attachmentObject,attachmentObject2)
Use mailR - it works with authentication, attachments, it automatically send txt message along with html and more.
mailR requires rJava which can be a bit of a pain sometimes. On windows I haven't had any problems. On ubuntu this solved the one issue I've had:
sudo apt-get install openjdk-jdk
in R
install.packages("devtools", dep = T)
library(devtools)
install_github("rpremraj/mailR")
(if you have trouble with rJava - try sudo R CMD javareconf in terminal)
mailR is easy to work with and well documented on the github page.
Example from the documentaion
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)
Note: your smtp server might find excessive use suspicious. This is the case with e.g. gmail. So after sending a few mails you probably have to log in to the gmail account and see if the account has been temporarily disabled. Also note that if you use a gmail account with two-factor authentication you need to use an application specific password.
Would you settle for a twitter message? You could use Rcurl to post an update to twitter, which can then be forwarded to your cell phone as a text, or to your email via the notification settings.
See here: http://www.sakana.fr/blog/2007/03/18/scripting-twitter-with-curl/
Have you looked into the sendmailR package yet? It allows SMTP to submit a message and you could probably edit the function to allow an attachment. Then again, if its only one log file it might just be worth it to use shell() as you mentioned.
For Windows one might parse together a VB-Script (see e.g. http://www.paulsadowski.com/wsh/cdo.htm ) and then call it via shell.
This might look like this:
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")
}
Just want to remind people who wants self-notifying feature of a service called twilio, they provide free service to send sms to your own cellphone. A walk-through using R is available here https://dreamtolearn.com/ryan/data_analytics_viz/78
An example code is attached, just replace the credentials with the ones of your own.
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"))
Here is a simple code snippet for sending an e-mail by using "mailR" package
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()
})
For those who suggested using 'mailR' - this is the way to go, but it is difficult to install of ubuntu. Here is a resource to install 'javaR' on ubuntu which will allow you to install 'mailR'
https://www.r-bloggers.com/2018/02/installing-rjava-on-ubuntu/
Late to this, but you can avoid shelling out to vbscript by using RDCOMClient correctly, as in this example from 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
}

Resources