rpremraj / mailR

A utility to send emails from the R programming environment
http://rpremraj.github.io/mailR/
190 stars 57 forks source link

When HTML = TRUE, writing body to contain text and HTML #41

Closed krby2821 closed 8 years ago

krby2821 commented 8 years ago

Hello,

I've been really impressed with this package so far and it's been really helpful!! I am trying to create a body of an email with an inline image and also include calculated text (using values stored in R). I'm able to send an image inline, which is a great starting point, but am having .

A simplified example to help illustrate this. This script would run once each morning, like a daily sales report:

Run SQL query with ODBC package to get dataframe of daily sales by store

save result as sql_result

plot sales by hour and department

save graph as sales_graph.png in working directory

Compose email

library(mailR) subject=paste("Sales report for",Sys.date(),sep=" ") send.mail(from = "me@gmail.com", to = "them@gmail.com", subject = subject, body = c(paste("Average daily sales = $", sum(sql_df$sales_data),sep=""), '<.img src="Sales_graph dot png">', html = TRUE, inline = TRUE, smtp = list(host.name = ""), attach.files=c("Sales_graph dot png"), authenticate = FALSE)

Since the result of the sql query and sum of sales changes each day, I want to include the ability to calculate it on the fly and include the value in the body of the email.

When I try to do something like this, I get the error: In if (file.exists(body)) body <- readChar(body, file.info(body)$size) : the condition has length > 1 and only the first element will be used

Any idea how to make this work, or if this change is on a development roadmap?

rpremraj commented 8 years ago

Hi --- glad that you like mailR.

Above mailR is complaining that the body parameter is not a single string, nor is it a file in your file system. So fixing this parameter will certainly do the trick.

But I recommend you use the infuser package to achieve this. You can handle more complex cases with ease. For example, infuse("Average daily sales = {{daily}}", list(daily = 3)) will result in string Average daily sales = 3.