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)
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?
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)
I am trying to send an email from a secondary email address using RDCOMClient. I took the advice from How to retrieve Outlook inbox emails using R RDCOMClient? and tried writing it in VBA and translating, but could not get the right commands.
Note: I can't use SentOnBehalfOfName because I don't have the necessary permission.
The below VBA and Python code both successfully send email from the secondary inbox.
VBA
Sub SendUsingAccount()
Dim oAccount As Outlook.Account
Dim oMail As Outlook.MailItem
Set oAccount = Application.Session.Accounts.Item(2) 'Index of Mailbox
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using MAPI Account"
oMail.Recipients.Add "email#email.com"
oMail.Recipients.ResolveAll
oMail.SendUsingAccount = oAccount
oMail.Send
End Sub
Python
import win32com.client
o = win32com.client.Dispatch("Outlook.Application")
oacctouse = None
for oacc in o.Session.Accounts:
if oacc.SmtpAddress == "myemail#email.com":
oacctouse = oacc
break
#print oacc
#dir(oacc)
#oacc.CLSID
#oacc.GetAddressEntryFromID
Msg = o.CreateItem(0)
if oacctouse:
Msg._oleobj_.Invoke(*(64209, 0, 8, 0, oacctouse)) # Msg.SendUsingAccount = oacctouse
Msg.To="email#email.com"
Msg.HTMLBody = "test env instance #"
Msg.Send()
R
Things I have tried in R in addition to guessing all combinations I can think of for [["SMTP"]], $SmtpAddress, etc:
OutApp <- COMCreate("Outlook.Application")
outMail <- OutApp$CreateItem(0)
#1 :No Error, but email sends from primary inbox
oa<-OutApp[["Session"]][["Accounts"]]
second_inbox<-oa$Item(2)
outMail[["SendUsingAccount"]]=second_inbox
#2: Runs, but sends from primary inbox
outMail[["SendUsingAccount"]]="myemail#email.com"
#From what I read emails need to be accessed with a number,not the name
#3 Runs, but sends from primary inbox (the Python index changes every run)
outMail[["SendUsingAccount"]]="oacc_id_from_Python"
#Rest of reproducible code
outMail[["To"]] = "email#email.com"
outMail[["subject"]] = "Alt Acc"
outMail[["body"]] = "test"
outMail$Send()
Related questions:
https://social.msdn.microsoft.com/Forums/windows/en-US/7afc9e42-ca4f-491b-8c50-19556fb4e1cf/sendusingaccount-does-not-work-in-outlook-2010-possible-bug?forum=outlookdev
Sending email in R via outlook,
Ideas?
Source:
http://www.seancarney.ca/2020/10/07/sending-email-from-outlook-in-r/
# Send the message from an alternate account
Email[["sentonbehalfofname"]] = "alternate-sender#test.com"
This guy nailed it. :)
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))