0

In my PS script I have a string variable which is later used as a body of html email. I need to change encoding of the variable to UTF8 so that the text will be displayed correctly in the email. The variable is defined in the script, it's not read from any file. Can anybody help me to fix that?

The solution in Encode a string in UTF-8 didn't work for me.

JanFi86
  • 449
  • 10
  • 29
  • 1
    Possible duplicate of [Encode a string in UTF-8](https://stackoverflow.com/questions/23256911/encode-a-string-in-utf-8) – marsze Oct 22 '18 at 13:56
  • The "encoding" of a string variable cannot change. But maybe this can help you: https://stackoverflow.com/questions/16255487/encoding-to-utf-8-in-email – marsze Oct 22 '18 at 13:58
  • 3
    The encoding of the variable is UTF-16 internally, but it shouldn't matter. Strings do not conceptually have encodings until they're converted to bytes at some point. If you're getting "wrong" characters, you need to identify the point where encoding characters to bytes might be going wrong. Focusing on the variable is the wrong thing to do. – Jeroen Mostert Oct 22 '18 at 13:58
  • thank you for bringing it up might help to some, I've tried the solution but didn't work for me (Encode a string in UTF-8 ) – JanFi86 Oct 22 '18 at 13:59
  • If it didn't work for you, you'll have to include more detail in your question as to what, exactly, isn't working. Otherwise it's impossible to tell how to fix things, other than that changing the encoding of a variable isn't possible and isn't the solution. – Jeroen Mostert Oct 22 '18 at 14:16

1 Answers1

1

I solved it with -encoding "UTF8".

  Send-MailMessage    -From $FromEmail `
                -to $mailto `
                -Subject $mailSubject `
                -Body $EmailBody `
                -SmtpServer $SMTPHost `
                -port $SMTPPort `
                -UseSsl `
                -Credential $cred `
                -BodyAsHtml `
                -Encoding "UTF8"
user3519275
  • 138
  • 2
  • 9