Send-MailMessage encoding issue in Powershell 4.0

This topic contains 3 replies, has 2 voices, and was last updated by  Johan Svensson 3 years, 10 months ago.

  • Author
  • #17252

    Johan Svensson

    Hey, hope someone can give me some advice on a weird issue i've encountered.
    I'm trying to send emails that contains swedish symbols using send-mailmessage in powershell 4.0. This works as intended when I run the command in a powershell console window but when I run the same cmdlet in a .ps1 file the swedish characters turn to gibberish.

    Send-MailMessage -subject 'Testing special characters: Å Ä Ö' -Body 'testing Å Ä Ö åäö' -to $to -from $from -encoding UTF7 -smtpserver $smtpserver

    The resulting mail when ran in ps1 file has the following body:
    testing ÅÄÖ åäö

    I then tried the .ps1 file on a win2008R2 server that has powershell 3.0 installed, and from this machine the email arrived correctly formatted.

    Have I encountered a bug in PS 4.0 or am I missing something obvious here?

    Cheers, Johan

  • #17262

    Martin Nielsen

    I can't seem to replicate your issue using a .ps1 file with the following

    Send-MailMessage -subject 'Testing special characters: Å Ä Ö' -Body 'testing Å Ä Ö åäö' -To $email -From $email -Encoding utf7 -SmtpServer $smtpserver -Credential (Get-Credential)

    I tried saving the .ps1 file in different encoding formats (ANSI/Unicode/UTF8) but no hieroglyphs.

    Perhaps something else in your script is causing the characters to get garbled?

  • #17263

    Johan Svensson

    Thanks Martin, I tried it on a windows 2012 r2 server with 4.0 installed and can't replicate it there either.
    That line is all my script file contains at the moment in order to make sure there was nothing else that could cause this.

    I'll run this script from the 2012 server but it makes me frustrated when I can't figure out why it's working differently on my client 🙂

  • #17264

    Johan Svensson

    Solved, I was using Notepad++ to save this file. Notepad++ was set to save as encoding "ANSI as UTF-8", when I changed this to UTF-8 it works perfectly. D'oh!

You must be logged in to reply to this topic.