Sending email in R via outlook
It is possible to send emails in R via Outlook. sendmailR works for me on Windows 7 and Outlook 2010. I referenced http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf
smtpServer= info for Outlook 2010 is in File -> Account Settings -> Account Settings -> double click your account -> text in "Server" box
library(sendmailR)
#set working directory
setwd("C:/workingdirectorypath")
#####send plain email
from <- "[email protected]"
to <- "[email protected]"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="serverinfo")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
#####send same email with attachment
#needs full path if not in working directory
attachmentPath <- "subfolder/log.txt"
#same as attachmentPath if using working directory
attachmentName <- "log.txt"
#key part for attachments, put the body and the mime_part in a list for msg
attachmentObject <- mime_part(x=attachmentPath,name=attachmentName)
bodyWithAttachment <- list(body,attachmentObject)
sendmail(from=from,to=to,subject=subject,msg=bodyWithAttachment,control=mailControl)
In addition, multiple files can be sent by adding another mime_part to the msg list as follows (I also condensed it):
attachmentObject <- mime_part(x="subfolder/log.txt",name="log.txt")
attachmentObject2 <- mime_part(x="subfolder/log2.txt",name="log2.txt")
bodyWithAttachment <- list(body,attachmentObject,attachmentObject2)
Old question, but if somebudy wants to use mailR for this task have a look at Settings (in outlook) than click "E-Mail" (below "Your App Settings") and navigate to "POP and IMAP" .. you will find hostname and port for SMTP here.
You can also use vbscript to expose all the functionalities of Outlook
Reference: MailItem Members (Outlook) - MSDN - Microsoft
################################################
#Outlook Enumerations
################################################
#OlMailRecipientType Enumeration (Outlook)
OlMailRecipientType <- c(olBCC=3, olCC=2, olOriginator=0, olTo=1)
#OlBodyFormat Enumeration (Outlook)
OlBodyFormat <- c(olFormatHTML=2, olFormatPlain=1, olFormatRichText=3, olFormatUnspecified=0)
#OlImportance Enumeration (Outlook)
OlImportance <- c(olImportanceHigh=2, olImportanceLow=0, olImportanceNormal=1)
#OlSensitivity Enumeration (Outlook)
OlSensitivity <- c(olConfidential=3, olNormal=0, olPersonal=1, olPrivate=2)
#OlDefaultFolders Enumeration (Outlook)
OlDefaultFolders <- c(olFolderCalendar=9, olFolderConflicts=19, olFolderContacts=10, olFolderDeletedItems=3,
olFolderDrafts=16, olFolderInbox=6, olFolderJournal=11, olFolderJunk=23,
olFolderLocalFailures=21, olFolderManagedEmail=29, olFolderNotes=12, olFolderOutbox=4,
olFolderSentMail=5, olFolderServerFailures=22, olFolderSyncIssues=20,
olFolderTasks=13, olFolderToDo=28, olPublicFoldersAllPublicFolders=18, olFolderRssFeeds=25)
################################################
#function to send email using Outlook
################################################
outlookSend <- function(To, Subject='', CC=NULL, BCC=NULL,
HTMLBodyFile=NULL, HTMLBody=NULL, Body='', Attachments=NULL,
DeferredDeliveryTime=NULL, OriginatorDeliveryReportRequested=NULL, ReadReceiptRequested=NULL,
FlagRequest=NULL, Importance=NULL, Sensitivity=NULL,
SentOnBehalfOfName=NULL)
{
vbscript <- c('Dim objOutlook',
'Dim objMailItem',
'Dim objFileSystem',
'Dim objNamespace',
'Dim objSentFolder',
'Set objOutlook = CreateObject("Outlook.Application")',
'Set objMailItem = objOutlook.CreateItem(0)',
'With objMailItem',
paste0('\t.Subject = "', Subject, '"'),
#Add recipients
unlist(lapply(To, function(recip) c(
paste0('\tSet recip = .Recipients.Add ("',recip,'")'),
paste('\trecip.Type =',OlMailRecipientType['olTo'])))),
if (!is.null(CC)) {
unlist(lapply(CC, function(recip) c(
paste0('\tSet recip = .Recipients.Add ("',recip,'")'),
paste('\trecip.Type =',OlMailRecipientType['olCC']))))
},
if (!is.null(BCC)) {
unlist(lapply(BCC, function(recip) c(
paste0('\tSet recip = .Recipients.Add ("',recip,'")'),
paste('\trecip.Type =',OlMailRecipientType['olBCC']))))
},
'\t.Recipients.ResolveAll',
#Insert email body
if (!is.null(HTMLBodyFile)) {
c(paste('\t.BodyFormat =', OlBodyFormat['olFormatHTML']),
'\tSet objFileSystem = CreateObject("Scripting.FileSystemObject")',
paste0('\tSet file = objFileSystem.OpenTextFile("',HTMLBodyFile,'", 1)'),
'\t.HTMLBody = file.ReadAll')
} else if (!is.null(HTMLBody)) {
c(paste('\t.BodyFormat =', OlBodyFormat['olFormatHTML']),
paste0('\t.HTMLBody = "',HTMLBody,'"'))
} else {
c(paste('\t.BodyFormat =', OlBodyFormat['olFormatPlain']),
paste0('\t.Body = "', Body, '"'))
},
#Add attachments
if (!is.null(Attachments)) {
vapply(Attachments, function(x) paste0('\t.Attachments.Add "',x,'"'), character(1))
},
#copy of the mail message is saved down
'\t.DeleteAfterSubmit = False',
#Delay delivery in minutes
if (!is.null(DeferredDeliveryTime)) {
paste0('\t.DeferredDeliveryTime = dateadd("n", ',DeferredDeliveryTime,', Now)')
},
#Indicates the requested action for a mail item
if (!is.null(FlagRequest)) {
'\t.FlagRequest = "Follow up"'
},
#Indicates the relative importance level for the mail item
if (!is.null(Importance)) {
paste('\t.Importance =', OlImportance[Importance])
},
#OriginatorDeliveryReportRequested of type logical: whether to receive delivery report
if (!is.null(OriginatorDeliveryReportRequested) && OriginatorDeliveryReportRequested) {
'\t.OriginatorDeliveryReportRequested = True'
},
#ReadReceiptRequested of type logical: request read receipt
if (!is.null(ReadReceiptRequested) && ReadReceiptRequested) {
'\t.ReadReceiptRequested = True'
},
#Indicates the the display name for the intended sender of the mail message
if (!is.null(SentOnBehalfOfName)) {
paste('\t.SentOnBehalfOfName =', SentOnBehalfOfName)
},
#Indicates the sensitivity for the Outlook item
if (!is.null(Sensitivity)) {
paste('\t.Sensitivity =', OlSensitivity[Sensitivity])
},
'End With',
'objMailItem.Send',
'Set objFileSystem = Nothing',
'Set objMailItem = Nothing',
'Set objOutlook = Nothing')
vbsfile <- tempfile('vbs',getwd(),'.vbs')
write(vbscript, vbsfile)
shell(paste('cscript /NOLOGO', vbsfile))
unlink(vbsfile)
} #outlookSend
#Examples Usage:
outlookSend(To=c("[email protected]","[email protected]"),
Subject="XXX", HTMLBodyFile='C:/Users/XXX/Documents/XXX.html',
Attachments=c('C:/Users/XXX/Documents/XXX.html', 'C:/Users/XXX/Documents/XXX.txt'))
This solution will be useful when you are blocked from sending emails directly through the SMTP server.
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"]] = "[email protected]"
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"]] = "[email protected]"