Using the encoding parameter with send-mailmessage to handle local characters in Powershell

I've begun using the the Powershell 2-introduced send-mailmessage. This has brought with it some problems for me with special characters like æ, ø and å.
 
I've tried different approaches like including the line: <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'> into the html-string (no succes). I've also written a function that handles translation of some of the chars, but this works only for the body - not for the subject.

I can't seem to understand the syntax of the -encoding parameter in send-mailmessage, which i beleave is the turning point in this matter.

Can anyone help me here?
function EncodeHTMLSpecialChars ([string]$Streng)
{
	$Streng = $Streng.Replace("æ", "&aelig;");
	$Streng = $Streng.Replace("ø", "&oslash;");
	$Streng = $Streng.Replace("å", "&aring;");
	$Streng = $Streng.Replace("Æ", "&AElig;");
	$Streng = $Streng.Replace("Ø", "&Oslash;");
	$Streng = $Streng.Replace("Å", "&Aring;");
	return $Streng
}

Open in new window

LVL 1
jmateknikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anilallewarCommented:
We had the same issue with our page when we want to have the browser display special characters. We got over that by displaying the ASCII code of the character at required place.

function EncodeHTMLSpecialChars ([string]$Streng)
{
      $Streng = $Streng.Replace("æ", "&#230;");
--------------and so on
}

Here is the reference to the page for ASCII character codes
http://www.web-source.net/symbols.htm
0
jmateknikAuthor Commented:
It still only works for the body (not for subject):

Subject should be: Din adgangskode er blevet ændret
Before your post: 'Din adgangskode er blevet &aelig;ndret'
After your post: 'Din adgangskode er blevet &#230;ndret'

For the curious reader it means 'Your password has been changed'.
0
anilallewarCommented:
It is a subject of the email or something to be shown on the page? As for as anything on the page, it would be shown correctly as the browser interprets these characters and shows the equivalent chararactes while loading the page.

Could you please provide some more context of what is happening now so that we may help you?
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

jmateknikAuthor Commented:
I' creating a script that sends an e-mail in the end via the ps2-cmdlet send-mailmessage.
When I do so I'm using parameters as per MS

Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[-SmtpServer] <string>] -From <string> [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc <string[]>] [-Credential <PSCredential>] [-DeliveryNotificationOption {<None> | <OnSuccess> | <OnFailure> | <Delay> | <Never>}] [-Encoding <Encoding>] [-Priority {<Normal> | <Low> | <High>}] [-UseSsl] [<CommonParameters>]

Out-of-the box -body and -subject changes local characters (æøå) to something else. To deal with it i wrote the function that takes and returns a string where special characters are replaced. The problem is that though both the body and subject string has been processed by the function the mail I receive shows the body correct - but the subject like:
'Din adgangskode er blevet &aelig;ndret'
where it should be:
'Din adgangskode er blevet ændret'

So I have a feeling that the -encoding parameter might be the salvation, though I can't seem to get the syntax working.

Please let me know if you have further questions...
0
anilallewarCommented:
It looks like your program is not displaying the mail correctly. Can you try opening the email using a browser based web-mail interface?

As per my understanding, the encoding would work for standard ASCII characters; but since you are using the extended character set, I am not sure if that would work.
0
jmateknikAuthor Commented:
It presents itself identically via webmail.

Try this code and you'll understand my problem.
$EmailTo      	= ""
$EmailFrom 		= ""
$EmailFromName 	= "ÆØÅ æøå"
$Subject		= "ÆØÅ æøå"

$SmtpHost       = ""
$Priority		= "High"

#$AdminHtmlBody = "<HTML><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'><Body>"
$HtmlBody 		= "<HTML><Body>"
$HtmlBody 		+= "<p>ÆØÅ æøå</p>"
$HtmlBody 		+= "</Body></HTML>"

function EncodeHTMLSpecialChars ([string]$Streng)
{
	$Streng = $Streng.Replace("æ", "&#230;");
	#$Streng = $Streng.Replace("æ", "&aelig;");
	$Streng = $Streng.Replace("ø", "&oslash;");
	$Streng = $Streng.Replace("å", "&aring;");
	$Streng = $Streng.Replace("Æ", "&AElig;");
	$Streng = $Streng.Replace("Ø", "&Oslash;");
	$Streng = $Streng.Replace("Å", "&Aring;");
	return $Streng
}

$HtmlBody 	= EncodeHTMLSpecialChars $HtmlBody
$Subject	= EncodeHTMLSpecialChars $Subject

Send-MailMessage -To $EmailTo -From "$EmailFromName <$EmailFrom>" -Subject $Subject -Body $HtmlBody -BodyAsHtml -Priority $Priority -SmtpServer $SmtpHost

Open in new window

0
jmateknikAuthor Commented:
I've solved it:

$encoding        = [System.Text.Encoding]::UTF8
Send-MailMessage -To $EmailTo -From "$EmailFromName <$EmailFrom>" -Subject $Subject -Body $HtmlBody -BodyAsHtml -Priority $Priority -Encoding $encoding -SmtpServer $SmtpHost

Help found at:
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/373eb9b2-33b8-40a2-9b47-07c20b0d1b30

Thanks to all that tried being at help
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.