I have successfully managed to implement the sendmailR function to send one message to one recipient.
Do you know if it is possible to send that same message to multiple recipients within the function? A form of CC'ing?
If not I think the only way is to loop round on a variable, which would normally be okay but for my current code would result with a loop within a loop and make things fairly and hopefully unnecessary complex
I cant see anything in the documentation that would appear to indicate functionality like this --> http://cran.r-project.org/web/packages/sendmailR/sendmailR.pdf
Thanks for any help, I will keep testing to see if there is a work around inm the meantime!
In the source code for sendmail it states...
if (length(to) != 1)
stop("'to' must be a single address.")
So this leaves you with several options (all of which are loops).The execution time of a loop compared to sending the email will be negligible. A couple of options are:
Option 1
Use Vectorize to vectorise the to argument of sendmail, allowing you to supply a character vector of email addresses to send to...
sendmailV <- Vectorize( sendmail , vectorize.args = "to" )
emails <- c( "me#thisis.me.co.uk" , "you#whereami.org" )
sendmailV( from = "me#me.org" , to = emails )
Option 2
Using sapply to iterate over the a character vector of email addresses applying the sendmail function each time...
sapply( emails , function(x) sendmail( to = "me#me.org" , to = x ) )
You could try the development version of the mailR package available on github https://github.com/rpremraj/mailR
Using mailR, you could send an email in HTML format as below:
send.mail(from = "sender#gmail.com",
to = c("recipient1#gmail.com", "recipient2#gmail.com"),
cc = c("CCrecipient1#gmail.com", "CCrecipient2#gmail.com"),
subject = "Subject of the email",
body = "<html>The apache logo - <img src=\"http://www.apache.org/images/asf_logo_wide.gif\"></html>",
html = TRUE,
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
This does the trick for me:
Define from, msg, subject, body seperatly:
from <- sprintf("<sendmailR#%s>", Sys.info()[4])
.....
TO <- c("<adres1#domain.com>", "<adres2#domain.com>")
sapply(TO, function(x) sendmail(from, to = x, subject, msg, body))
Related
How to send HTML format email in R, without the need for Java?
The package mailR is no longer usable as it is dependent on Java. What are the alternate packages?
Note:
The following worked well with mailR and trying to replicate the same.
send.mail(from = fromEmailAddress,
to = mailTo,
subject = subjectDetailed ,
body = bodyToSend,
html = TRUE,
smtp = list(host.name = hostname, port = 25),
send = TRUE
)
blastula, Microsoft365R are couple of great packages for HTML emails and also support Rmarkdown report emails
[blastula]
https://github.com/rstudio/blastula
[Microsoft365R]
https://github.com/Azure/Microsoft365R
The following works.
library(sendmailR)
msg = mime_part(bodyToSend)
msg[["headers"]][["Content-Type"]] = "text/html"
sendmail(mailFrom,mailTo,subject, msg, control=list(smtpServer= "smtpserver"))
I'm trying to make a massive mail sending campaign, but I'm struggling with the body argument of the function mailR (library mailR). There are many things I would like to be able to do, for example break lines, bold text, even inserting images.
An important thing to take into account is the fact that the e-mails are personalized according to a data frame, so I need to use variables. Right now I'm using the paste0 function.
This is an example:
for(i in 1:nrow(df)){
send.mail(from = "someone#gmail.com",
to = df$mail[i],
subject = "Campaign",
body = paste0("Beloved customer ", df$name[i], "remember that your member suscription will expire in ", df$days_until_expiration[i], "days. Greetings!"),
html = TRUE,
authenticate = TRUE,
smtp = list(host.name = "*****",
port = ***,
user.name = "someone#gmail.com",
passwd = "*****",
ssl = TRUE),
send = TRUE)
}
So that's the thing, is there any package that can help me customize the body of the e-mail?
I need to dynamically make a call to an API using the following format:
auth_secret <- paste0("Bearer ", secret)
headers = c(
`Authorization` = auth_secret,
`Notion-Version` = '2022-02-22',
`Content-Type` = 'application/json' )
res <- httr::PATCH(url = paste0('https://api.notion.com/v1/pages/', id),
httr::add_headers(.headers = headers),
body = payload,
encode = "json")
d <- httr::content(res)
This payload works:
payload <- "{\"properties\":{\"Project\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"
But if I want to create it dynamically, using a paste0 (so it is inside of a function), I get some backslashes added before and after:
payload <- paste0('"{\"properties\":{\"',property_name,'\":{\"relation\":[{\"id\":\"',value,'\"}]}}}"')
print(payload)
"\"{\"properties\":{\"%7CAK%5E\":{\"relation\":[{\"id\":\"8cb9519e72ca4bbe9e0448807acb8e10\"}]}}}\""
I presume this is due to some weird escape character being added but have run out of ideas. I've added two \ and have gotten same issue. Call fails as JSON is not passed correctly. Is there a workaround?
This is obviously something to do with the fact that paste0 is escaping even the double quotes you do not want to escape.
No doubt someone is aware of the details. However, when I get weird stuff with paste0, I just use sprintf, which seems to work in this case:
property_name = "Projects"
value = "1d148a9e-783d-47a7-b3e8-2d9c34210355"
payload <- sprintf(
"{\"properties\":{\"%s\":{\"relation\":[{\"id\":\"%s\"}]}}}",
property_name, value
)
print(payload)
# [1] "{\"properties\":{\"Projects\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"
I am trying to call the stats of a list of players from the call of duty API. This API requires firstly the login in website https://profile.callofduty.com/cod/login. Once logged in, the user can see the stats of a player using the call-of-duty API. For example, the stats of the streamer savyultras90 from Warzone can be seen through the following link: https://my.callofduty.com/api/papi-client/stats/cod/v1/title/mw/platform/psn/gamer/savyultras90/profile/type/wz.
If I log in from the website and try to see the stats of a player and the related json, I am able to do via browser. However, this doesn't seem straightforward in R.
I try to log in using the GET function from httr package as follows
respo <- GET('https://profile.callofduty.com/cod/login', authenticate('USER', 'PWD'))
But when I try to have access to the api and download the JSON file using the function fromJSON from the package jsonlite as follows
data <- fromJSON('https://my.callofduty.com/api/papi-client/stats/cod/v1/title/mw/platform/psn/gamer/savyultras90/profile/type/wz')
I get the error message "Not permitted: not authenticated".
How can I authenticate in one website and stay logged in to call from the API which relies on that authentication?
Seeing I've recently had to develop a PHP API for Warzone, I might be able to guide you in the right direction on how to handle this. But first a few remarks:
You need to authenticate each user individually with the appropriate platform if you want to request that player's data
There is a throttle limit on the amount of API requests
The API of Call of Duty is under strict usage guidelines and should only be used by registered partners. Making usage of the API could result in claims and eventually lawsuits: link
There is no public documentation of the API and the API has changed in the past, breaking several 3rd party tools.
Nevertheless, the process involves several steps as described below:
Register the device making the call
https://profile.callofduty.com/cod/mapp/registerDevice
with a json body in the form of {"deviceId":"INSERT_ID_HERE"}
This will return a response with the authHeader which we will use for as Token in the next calls
Login with Activision credentials
https://profile.callofduty.com/cod/mapp/login
Set the following headers:
Authorization: "INSERT_AUTHHEADER_HERE"
x_cod_device_id: "INSERT_PREVIOUSLY_USED_DEVICEID_HERE"
This in terms will generate a dataset where we will save the following data from:
rtkn, ACT_SSO_COOKIE and atkn.
Make the wanted API call for data
We have all the data now required to make the API call.
For each request we will submit 3 headers:
Authorization: "INSERT_AUTHHEADER_HERE"
x_cod_device_id: "INSERT_PREVIOUSLY_USED_DEVICEID_HERE"
Cookie: ACT_SSO_LOCALE=en_GB;country=GB;API_CSRF_TOKEN=**GENERATE_CSRF_TOKEN**;rtkn=**RTKN_HERE**;ACT_SSO_COOKIE=**ACT_SSO_COOKIE_HERE**;atkn=**ATKN_HERE**
For more reference, you can always look through a Python library or NodeJS library which succesfully implemented the API.
I struggled with this yesterday but finally made some progress. The issue is that you have to obtain an authentication token. The steps can be followed here: https://documenter.getpostman.com/view/7896975/SW7aXSo5#a37a2e5b-84bb-441d-b978-0fd8d42ffd29 but not available in R though.
My code works at first, as long as you don't authenticate again (still trying to figure out why). Basically what I did was to translate the steps in the link and extracted the content in the response from GET:
# Get token ---------------------------------------------------------------
resp <- GET('https://profile.callofduty.com/cod/login')
cookies = c(
'XSRF-TOKEN' = resp$cookies$value[1]
,'new_SiteId' = resp$cookies$value[2]
,'comid' = resp$cookies$value[3]
,'bm_sz' = resp$cookies$value[4]
,'_abck' = resp$cookies$value[5]
# ,'ACT_SSO_COOKIE' = resp$cookies$value[6]
# ,'ACT_SSO_COOKIE_EXPIRY' = resp$cookies$value[7]
# ,'atkn' = resp$cookies$value[8]
# ,'ACT_SSO_REMEMBER_ME' = resp$cookies$value[9]
# ,'ACT_SSO_EVENT' = resp$cookies$value[10]
# ,'pgacct' = resp$cookies$value[11]
# ,'CRM_BLOB' = resp$cookies$value[12]
# ,'tfa_enrollment_seen' = resp$cookies$value[13]
)
headers = c(
)
params = list(
`new_SiteId` = 'cod',
`username` = 'USER',
`password` = 'PWD',
`remember_me` = 'true',
`_csrf` = resp$cookies$value[1]
)
# Authenticate ------------------------------------------------------------
resp_post <- POST('https://profile.callofduty.com/do_login?new_SiteId=cod',
httr::add_headers(.headers=headers),
query = params,
httr::set_cookies(.cookies = cookies))
cookies = c(
'XSRF-TOKEN' = resp_post$cookies$value[1]
,'new_SiteId' = resp_post$cookies$value[2]
,'comid' = resp_post$cookies$value[3]
,'bm_sz' = resp_post$cookies$value[4]
,'_abck' = resp_post$cookies$value[5]
,'ACT_SSO_COOKIE' = resp_post$cookies$value[6]
,'ACT_SSO_COOKIE_EXPIRY' = resp_post$cookies$value[7]
,'atkn' = resp_post$cookies$value[8]
,'ACT_SSO_REMEMBER_ME' = resp_post$cookies$value[9]
,'ACT_SSO_EVENT' = resp_post$cookies$value[10]
,'pgacct' = resp_post$cookies$value[11]
,'CRM_BLOB' = resp_post$cookies$value[12]
,'tfa_enrollment_seen' = resp_post$cookies$value[13]
)
headers = c(
)
params = list(
`new_SiteId` = 'cod',
`username` = 'USER',
`password` = 'PWD',
`remember_me` = 'true',
`_csrf` = resp_post$cookies$value[1]
)
# Get data:
resp_psn <- httr::GET(url = 'https://my.callofduty.com/api/papi-client/stats/cod/v1/title/mw/platform/psn/gamer/savyultras90/profile/type/wz',
httr::add_headers(.headers=headers),
query = params,
httr::set_cookies(.cookies = cookies))
resp_psn_json <- content(resp_psn)
Let me know if you've already managed to resolve this!
I am trying to send a mail about using mailRpackage, but as it is quite complicated issue, I would like to add a bit of formaring:
"Do not cut this forest!" should be bold
Numbered list should be nicely formatted
.
library(mailR) # library used to send mails
# The text I would like to send:
Text <- "Hi!
Do not cut this forest!
The reason for this ar as follows:
1. Trees are good
2. bla bla bla
best regards,
MS"
#In reality I am reading it from TXT file
text_real <- readChar('text_real.txt', file.info('text_real.txt')$size)
text_real <- enc2utf8(text_real)
sender <- ...
recipients <- ...
password <- ...
title <- "title"
#Sending mail
send.mail(from = sender,
to = recipients,
subject = title,
body = Text,
encoding = "utf-8",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = sender,
passwd = password, ssl = TRUE),
authenticate = TRUE,
send = TRUE)
Alternativly I know that mailR is using html so I have written it all in World and saved is as html. Unfortunatly it didn't work and coused seccond problem
Text <- paste(readLines("real text.htm"), collapse="\n")
body of mail in fact send::
< html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">
...
I need to personalise every mail, in first solution (the txt one) I have used simple gsub function and I believe it will not be working here.
I have also tried adding to TXT file, html formatting (<b> and <strong>) manually, but it didn't work.
Thanks!
Creating a html was correct, but you don't read it into R. send.mail()can send html-files as body.
library(mailR)
send.mail(from = sender,
to = recipients,
subject = title,
body = "path-to-html-file", #pass the file
html = TRUE, #tell send.mail you're using html
encoding = "utf-8",
smtp = list(host.name = "smtp.gmail.com", port = 465,
user.name = sender,
passwd = password, ssl = TRUE),
authenticate = TRUE,
send = TRUE)