How to Send an Email With Attachment from R in Windows

Sending email attachment in R via Outlook

Your issue lies in the fact that you have not escape the escape when adding the attachment

You have the below

 outMail[["Attachments"]]$Add("A:/Automate_Emails/Test_Attachment.pdf")

You it should look like this

 outMail[["Attachments"]]$Add("A:\\Automate_Emails\\Test_Attachment.pdf")

Sending email in R via outlook

You can use RDCOMClient package to access to COM objects from within R. You can easily access the Application Object (Outlook) and configure it. Here a simple example of sending an email:

library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "dest@dest.com"
outMail[["subject"]] = "some subject"
outMail[["body"]] = "some body"
## send it
outMail$Send()

Of course, this assumes that you have already install outlook and configure it to send/receive your emails. Adding attachment is quite simple also:

outMail[["Attachments"]]$Add(path_to_attch_file)

sending on behalf of secondary mailbox:

outMail[["SentOnBehalfOfName"]] = "yoursecondary@mail.com"

sendmailR (Part2): Sending files as mail attachments

With the mailR package (https://github.com/rpremraj/mailR), you could send HTML emails and additionally attach files with ease as below:

send.mail(from = "sender@gmail.com",
to = c("recipient1@gmail.com", "recipient2@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),
attach.files = c("./download.log", "upload.log"),
authenticate = TRUE,
send = TRUE)

Edit (2014-05-13):

mailR has been updated to allow different character encodings. Below is an example to send the message as UTF-8.

send.mail(from = "Sender Name <sender@gmail.com>",
to = "recipient@gmail.com",
subject = "A quote from Gandhi",
body = "In Hindi : थोडा सा अभ्यास बहुत सारे उपदेशों से बेहतर है।
English translation: An ounce of practice is worth more than tons of preaching.",
encoding = "utf-8",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = T),
authenticate = TRUE,
send = TRUE)

Send authenticated mails via Outlook through R using mailR package

This took me a while to figure out. Try this:

send.mail(from = "username@custom.org",
to = c("recipient1@custom.org", "recipient2@custom.org"),
subject = "Title",
body = "Hello from R.",
authenticate = TRUE,
smtp = list(host.name = "smtp.office365.com",
port = 587,
user.name = "username@custom.org",
passwd = "Pa55w0rd",
tls = TRUE))

It is a common misconception that the port is 25 or 447. I believe port 25 can only be used whenauthenticate = FALSE.

Many sources claim that the correct server is smtp-mail.outlook.com. Perhaps you could try this in the event that the code does not work. Moreover, do not use ssl = TRUE. It has to be tls = TRUE.

Shoutout to Rahul Premraj's answer to this archived 2014 question.

Email an attachment in R with gmail

A response that works and works well is found here:

http://r.789695.n4.nabble.com/Email-out-of-R-code-td3530671.html

Thank you to nutterb for an answer from the rhelp list. Thank you to all that tried to assist me and were patient with my ignorance of python.



Related Topics



Leave a reply



Submit