[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8103
  • Last Modified:

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

0
jmateknik
Asked:
jmateknik
  • 4
  • 3
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now