1

I'm having trouble getting my email's body text in UTF-8. My markdown report is ok, and the script works when running from RStudio - i.e body text is in UTF-8. My problem is that when I run the script from command line my email message is encoded using windows-1252 which I don't really want.

How do I set up my code to specify my emails header to have content type UTF-8? InternetCodepage doesn't work atleast

R-code below:

## Bodytext
bodyMail <- paste(__My UTF-8 message goes here__, sep  = "")

# init com api
OutApp <- COMCreate("Outlook.Application")

# Create email
outMail = OutApp$CreateItem(0)

# Params 
outMail[["InternetCodePage"]] = "65001"
outMail[["To"]] = __your_outlook_email___
outMail[["subject"]] = "Subject_text"
outMail[["BodyFormat"]] = "2"
outMail[["HTMLBody"]] = bodyMail
outMail[["Attachments"]]$Add(__path_to_html_report__)

## send it                     
outMail$Send()
ErrantBard
  • 1,421
  • 1
  • 21
  • 40

1 Answers1

1

All string properties in Outlook (and all other IDispatch-friendly COM libraries) are UTF-16. It is your responsibility to make sure that you pass the right data.

On a side note, it is a good idea to HTML-encode all characters outside of the normal ASCII range. This way the code page will not matter at all.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I don't really understand, the text I want to pass is the right data. What I want to be able to do is to change the encoding in the html header for the email that I create above. At the moment it is stuck at windows 1252. I did html-encode my ö:s and ä:s, a stop-gap solution but not optimal for future maintenance. R does what it can encoding wise, it's me who need to know what to tell outlook and how. – ErrantBard Sep 15 '16 at 05:56
  • The problem is not with the code above, I guess, since it works fine as long as I stick to R. It's when I involve the cmd that the encoding is lost/transformed. In this case it might be ok to not write the ä and ö's in non human-readable signs, since there are so few. But there might be need to write longer body text's in the future and a nice feature would be to have them human-readable from scratch. – ErrantBard Sep 15 '16 at 06:47
  • HTML-encoding all extended characters is the optimal solution, this way you will not depend on anything. And how would R know what the encoding of your string is when it converts to UTF-16? Conversions like that are your responsibility if you don't want any nasty surprises. – Dmitry Streblechenko Sep 15 '16 at 13:45
  • Hmm, I understand. It makes the text a tad harder to understand though. It would be nice to have the text in the final form in my script as well. But it atleast fixes the problem so I'll take the answer. R (or rather RStudio) knows since I tell R what encoding it is(if I understand you correctly), this doesn't work when scheduling the script. – ErrantBard Sep 16 '16 at 06:19