Related
I am running into an issue with blastula email and SMTP Gmail.
When I run the following code on my local RStudio, it runs perfectly well and the email is sent.
library(blastula)
# To store my credentials with pwd in a file
create_smtp_creds_file(
file = "gmail_creds",
user = "myemail#gmail.com",
host = "smtp.gmail.com",
port = 465,
use_ssl = TRUE
)
email <- blastula::render_email("RMarkdown_Template.Rmd")
email %>%
smtp_send(
to = c("myother#email.com"),
from = c("Your Newsletter" = "myemail#gmail.com"),
subject = "Daily Newsletter",
credentials = creds_file("gmail_creds")
)
But when I run this exact same code on my RStudio on an ec2 instance, I get the following error message and the email is not sent:
Error in curl_fetch_memory(smtp_server, handle = h) : Login denied
FYI I activated the 'Less secure app' on the Gmail side.
Any idea where this problem come from?
I'm trying to send emails using R, which will eventually be deployed in a Shiny application. I've got an Office 365 account which is able to send email if I log into it but using the SMTP server I'm not getting any connection so far.
I've tried packages mailR & emayili but I'm not getting anywhere for some reason. I've used our local mail server before and that worked but that won't work for a shiny deployment.
According to Outlook I'm supposed to use:
Server name: smtp.office365.com
Port: 587
Encryption method: STARTTLS
But I can't seem to find a package that can do STARTTLS encryption. For other mail providers I see the advice to turn on "less secure apps" but that's not an option for us.
Example code :
library(mailR)
send.mail(from = "Me#companydomain.com",
to = c("you#companydomain.com"),
subject = "Test",
body = "TEST",
smtp = list(host.name = "smtp.office365.com",
port = 587,
user.name = "Me#companydomain.com",
passwd = "SuperSecretPassword",
tls = TRUE),
authenticate = TRUE,
send = TRUE,
debug = TRUE)
# Another option, with emayili:
library(emayili)
smtp <- server(host = "smtp.office365.com",
port = 587,
username = "Me#companydomain.com",
password = "SuperSecretPassword")
email <- envelope(from = "Me#companydomain.com",
to = "you#companydomain.com",
subject = "This is a plain text message!",
text = "Hello!")
smtp(email, verbose = TRUE)
I really hope someone figured out a package that can use STARTTLS or how to encrypt in that way using one of these packages!
Best Regards,
Bob
NOOB question related to R blastula.
I am trying to send an email with blastula using office365.
I was able to create a credentials file but can't seem to send a simple test email:
require(blastula) ; require(curl)
email <- compose_email(body = "Insert your e-mail body here", footer = "Insert your e-mail footer here")
email %>% smtp_send(from = "me#email.com", to = "someone_else#email.com", credentials = creds_file("C:/Users/me/CREDS_FILE")
)
There seems be a dependency on curl that's throwing the error:
Error in curl::curl_fetch_memory(url, handle = h) : MAIL failed: 530
What am I doing wrong ?
Try use_ssl = TRUE in create_smtp_creds_key. This worked for me.
email <- compose_email(
body = md(
"Your email message"
))
smtp <- create_smtp_creds_key(
id = "outlook",
user = "youremail#outlook.com",
provider = "office365",
host = "smtp.office365.com",
use_ssl = TRUE)
email %>%
smtp_send(to = to,
from = from,
subject = subject,
credentials = creds_key(id = "outlook"))
After doing some searching around I found this question and answer which seemed related. Hadley's suggestion of restarting R and reinstalling {curl} fixed the issue that I was having.
I actually reinstalled both {curl} and {blastula} with
install.packages(c('blastula', 'curl'))
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
}
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
}