Sending emails in R without outlook app in the system - r

I'm able to send emails using the following code.
OutlookForSend = RDCOMClient::COMCreate("Outlook.Application")
emailToSend = OutlookForSend$CreateItem(0)
emailToSend[["subject"]] = "Subject"
emailToSend[["HTMLBody"]] = bodyToSend
emailToSend[["To"]] = "Email"
emailToSend$Send()
However, I don't have outlook installed, in the server machine but still need to send emails.
I'm able to achieve the same using the package mailer in Python , what is the best way to achieve the same in R.
Thanks

Any SMTP client implemented in R will do the job.
Check out this one: Rmailer
From their example:
library(Rmailer)
message <- c(
"Hey,",
"",
"I have a nice pic for you!",
"",
"Best",
"C."
)
settings <- list(
server = "smtp.example.org",
username = "user",
password = "password"
)
## send message:
sendmail(
from = "sender#example.org",
to = "receiver#example.org",
subject = "Good news!",
msg = message,
smtpsettings = settings,
attachment = "nice_pic.jpg"
)

Solved the problem, using mailR package and it works well.
library(mailR)
send.mail(from = "email#company.com",
to = "email#company.com",
subject = subjectToSend ,
body = bodyToSend,
html = TRUE,
smtp = list(host.name = "smtp.company.com", port = 25),
send = TRUE)

Related

I need to send some emails in a loop changing receiver names in each iteration

I have a text file in one column are the receiver surnames, and on the other their email addresses. I am using the package mailR in R to send them a personalised text.
I have tried putting it in a for loop but it is not working.
install.packages("mailR", dep = T)
library(mailR)
email.addresses <- c("mail1#gmail.com","email2#yahoo.com","email3#hotmail.com")
names.addresses <- c("name1","name2","name3")
for (i in length(email.addresses)) {
dear <- "Dear "
comma <- ","
dr <- "Mr. "
name <- names.addresses[i]
subject <- paste(dear, dr, name, comma, sep = "")
email <- send.mail(from = "sender <user#gmail.com>",
to = email.addresses[i],
subject = "subject header",
body = paste(subject, "some text", sep = "\n\n"),
encoding = "utf-8",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = "username", passwd = "password", ssl = T),
authenticate = TRUE,
send = TRUE)
}
What gets actually sent is only to the last email address of the list email.addresses - "email3#hotmail.com".
Any ideas?
I just run your code and you should just change your for loop by :
for (i in 1:length(email.addresses))
Next time try to print your data (I mean by that to print the variable i) and you would have seen that your loop only does one loop, because length(email.addresses) = 3 but when using for loop you need to give 2 values, the begin and the end.
1:length(email.addresses) is the same as c(1:length(email.addresses)) and it means that it will take all the values between 1 and length(email.addresses).

R interface to imgflip API (https://api.imgflip.com/). Always ends in with the failure "No texts supplied"

A simple post using the httr package always ends with an error?
# Post a random meme and print its url
res <- httr::POST(
url = "https://api.imgflip.com/caption_image",
body = list(
template_id = "61579",
username = "<my-username>",
password = "<my-password>",
text0 = "abc",
text1 = "def",
font = "impact",
max_font_size = "50"
),
httr::verbose(),
encode = "json"
)
httr::content(res, "text")
Im not sure how to do it in R, but I tried using postman and sending the request as JSON do not work you have to send it as a form-data:

How to: New order Binance API via RStudio

I am trying to create a new order via the Binance API using RStudio.
I found the Binance Official API Docs and figured out that I should use
POST /api/v3/order (HMAC SHA256).
The following script doesn't work out for me:
url='https://api.binance.com/api/v3/account'
GET(url,
add_headers("X-MBX-APIKEY"= *[my API key]*),
query=list("symbol"="ETHBTC",
"side"="BUY",
"type"="MARKET",
"quantity"=1,
recvWindow=5000,
"timestamp"=1499827319559,
"signature"=**???**),
verbose())
Does anyone know what I'm doing wrong and how I can create an order via the Binance API using RSTUDIO and how I can create my signature?
library(httr)
timestamp <-
as.character(jsonlite::fromJSON(content(
GET("https://api.binance.com/api/v1/time"), "text"
))$serverTime + 999)
query <-
list(
"symbol" = "VENBTC",
"side" = "BUY",
"type" = "MARKET",
"quantity" = 1,
"recvWindow" = 5000,
"timestamp" = timestamp
)
signature <-
digest::hmac(
key = "*[my secret key]*",
object = paste(names(query), query, sep = "=", collapse = "&"),
algo = "sha256"
)
POST(
url,
add_headers("X-MBX-APIKEY" = "*[my API key]*"),
query = c(query, signature = signature),
verbose()
)

How to send email in R that read csv and send multiple email at once?

I have CSV file that contains email.
How to send multiple email in r ?
Error 1
send.mail function not taking data.frame values
Error in FUN(X[[i]], ...) :
Sorry, parameter type `NA' is ambiguous or not supported.
Error 2
function not taking input from read.table
Error in file.exists(body) : invalid 'file' argument
My code as below (credentials as an example only)
temp <- read.csv("E:/OneDrive/M.Tech/temp.csv",header = FALSE)
m <- data.frame(temp)
email <- m[2,14]
rollno <- m[2,1]
file <- read.table("C:/Mehul Katara/New folder (2)/131004.txt",header = TRUE,sep = ";")
send.mail(from = "user#gmail.com",
to = email,
subject = rollno,
body = file,
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "user#gmail.com", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
There is just one error with this. Use:
file <- read.table("C://Mehul Katara//New folder (2)//131004.txt",header = TRUE,sep = ";")
So you need to change / in path to // because of R windows binary behavior. Above path may also not work but in that case you need to get rid of spaces as in "MehulKatara". Please put your file in another place where there is no space in filepath. Error 1 is coming just because of error 2 and should go as soon as you fix error 2.
From the documentation
body Body of the email as text. If the parameter body refers to an
existing file location, the text of the file is parsed as body of the
email.
so ideally you should change call of function to something like:
send.mail(from = "user#gmail.com",
to = email,
subject = rollno,
body ="C://Mehul_Katara//New_folder_(2)//131004.txt"
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "user#gmail.com", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)

sending POST request to azure ML Web service using poster

I have successfully deployed a web service using Azure ML and am able to get output both on Azure ML as well as a sample R client application.
I would like to however get response using the firefox poster.
I have followed the instructions from the Azure page on deploying the web service and tried using the same request headers and parameters as follows
Instructions from azure page
this is what I've tried on Poster
Error message
My R Code which works
library("RCurl")
library("rjson")
# Accept SSL certificates issued by public Certificate Authorities
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
h = basicTextGatherer()
hdr = basicHeaderGatherer()
req = list(
Inputs = list(
"input1" = list(
"ColumnNames" = list("Smoker", "GenderCD", "Age"),
"Values" = list( list( "1", "M", "8" ), list( "1", "M", "8" ) )
) ),
GlobalParameters = setNames(fromJSON('{}'), character(0))
)
body = enc2utf8(toJSON(req))
api_key = "hHlKbffejMGohso5yiJFke0D9yCKwvcXHG8tfIL2d8ccWZz8DN8nqxh9M4h727uVWPz+jmBgm0tKBLxnPO4RyA=="
authz_hdr = paste('Bearer', api_key, sep=' ')
h$reset()
curlPerform(url = "https://ussouthcentral.services.azureml.net/workspaces/79f267a884464b6a95f5819870787918/services/e3490c06c73849f8a78ff320f7e5ffbc/execute?api-version=2.0&details=true",
httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
postfields=body,
writefunction = h$update,
headerfunction = hdr$update,
verbose = TRUE
)
headers = hdr$value()
httpStatus = headers["status"]
if (httpStatus >= 400)
{
print(paste("The request failed with status code:", httpStatus, sep=" "))
# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(headers)
}
print("Result:")
result = h$value()
print(fromJSON(result))
My API key
hHlKbffejMGohso5yiJFke0D9yCKwvcXHG8tfIL2d8ccWZz8DN8nqxh9M4h727uVWPz+jmBgm0tKBLxnPO4RyA==
How can I form a correct URL which works?
The "Content to Send" section is incorrect, you have specified URL-encoded, while you need to put application/json.

Resources