When I try to use sendmail from the sendmailR package in R, I am getting this error:
Error in .smtp_submit_mail(server, port, headers, msg, verbose) : argument "msg" is missing, with no default
Here is the code I am trying to execute:
library(sendmailR)
from <- "<some.address#gmail.com>"
to <- "<some.address#gmail.com>"
subject <- "this subject"
body <- "this text right here"
mailControl <- list(smtpServer = "ASPMX.L.GOOGLE.COM")
sendmail(from = from, to = to, subject = subject,
body = body, control = mailControl)
This is essentially the same code that can be found in online tutorials.
The parameter for the body of the email is called msg, not body. Use
sendmail(from = from, to = to, subject = subject,
msg = body, control = mailControl)
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 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)
I have a dataframe urls which is just a list of URLs that I want to crawl to obtain a variable pageName defined in the source code. For this purpose I use the following code:
# Crawl Page Names
for(n in 1:length(urls$URL))
{
if (domain(urls$URL[n])=="www.domain.com") {
doc = readLines(con = file(as.character(urls$URL[n]), encoding = "UTF-8"))
close(con)
rowNumber = grep('s.pageName', doc)
datalines = grep(pageNamePattern,doc[rowNumber],value=TRUE)
gg = gregexpr(pageNamePattern,datalines)
matches = mapply(getexpr,datalines,gg)
matches = gsub(" ", "", matches[1], fixed = TRUE)
result = gsub(pageNamePattern,'\\1',matches)
names(result) = NULL
urls$pageName[n] = stri_unescape_unicode(result[1])
} else {
urls$pageName[n] <- NA
}
}
if (domain(urls$URL[n])=="www.domain.com") uses the function domain included in the urltools package and let me crawl just those URLs where I know the pageName variable is defined, which are those in a specific domain.
However, my code is interrupted if the parsed page's http status response returns a 4XX Client Error or a 5XX Server error.
I would like to add a second if to the code for doing the crawl only if the http status response of con is 200 (OK). Does someone have an idea on how to do it or which package or functions to use?
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))