Link to home
Start Free TrialLog in
Avatar of jmateknik
jmateknikFlag for Denmark

asked on

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

Avatar of anilallewar
anilallewar
Flag of India image

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
Avatar of jmateknik

ASKER

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'.
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?
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...
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.
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

ASKER CERTIFIED SOLUTION
Avatar of jmateknik
jmateknik
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial