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.

5
  • 1
    Possible duplicate of Encode a string in UTF-8 Commented Oct 22, 2018 at 13:56
  • The "encoding" of a string variable cannot change. But maybe this can help you: stackoverflow.com/questions/16255487/encoding-to-utf-8-in-email Commented Oct 22, 2018 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. Commented Oct 22, 2018 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 ) Commented Oct 22, 2018 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. Commented Oct 22, 2018 at 14:16

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.