mailR icon indicating copy to clipboard operation
mailR copied to clipboard

Encoding problem when run r-script out of bat-file

Open woldemarg opened this issue 7 years ago • 1 comments

Here is a simple script for sending e-mail with a plain text in its body:

library(mailR)

send.mail(
  from = "[email protected]",
  to = "[email protected]",
  subject = "encoding test",
  body = "тест (ukrainian)\ntest (english)", #plain text in a body of an e-mail
  encoding = "utf-8",
  smtp = list(
    host.name = "smtp.gmail.com",
    port = 465,
    user.name = "myUsername",
    passwd = "myPassword",
    ssl = TRUE
  ),
  authenticate = TRUE,
  send = TRUE
)

Everything works perfect if I run this script out of my RStudio. But when it is started out of bat-file via Rscript an e-mail get messy content:

тест (ukrainian) test (english)

Here is my session info:

sessionInfo()

R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Russian_Russia.1251  LC_CTYPE=Russian_Russia.1251    LC_MONETARY=Russian_Russia.1251
[4] LC_NUMERIC=C                    LC_TIME=Russian_Russia.1251    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] mailR_0.4.1

woldemarg avatar Mar 18 '17 10:03 woldemarg

Almost a year on still have no solution for the problem. At least works the following tactics:

  • write your (html) letter into .csv
  • read it into dataframe like this
html <- read.csv("html_letter.csv",
                     encoding = "UTF-8",
                     stringsAsFactors = FALSE)
  • insert it paragraph by paragraph into the body of the letter in send.mail() like this:
body = paste(
        html$p1, # first paragraph from html_letter.csv
        var1, # if needed
        html$p2,
        var2,
        html$p3,
        ... ,
         sep = ""
      )

woldemarg avatar Jan 10 '18 19:01 woldemarg