How can I send German umlauts correctly with PowerShell Send-MailMessage?

0
0
Asked By MellowCedar42 On

I'm writing a PowerShell script that imports recipient and server information from a CSV, then sends personalized emails. The script works except that German characters such as Ä, ä, Ö, ö, Ü, and ü arrive as question marks. Normal emails from Outlook handle these characters correctly.

I've tried different encodings and experimented with BodyAsHtml, but the HTML version caused the script to fail. I also need to know whether variables can be included in an HTML body. The umlauts in my current test are hardcoded in the body, although other parts of the eventual message will come from the CSV.

The script creates a hashtable of parameters, including the subject, body, recipient, and SMTP server, and then calls Send-MailMessage. What is the correct way to apply UTF-8 encoding and pass the parameters so the characters are preserved?

4 Answers

Answered By PixelHarbor7 On

The main issue in the posted example is that the splat hashtable is created but never passed to Send-MailMessage. Call it with the splatting operator, and set Encoding there:

$sendMailMessageSplat = @{
From = '[email protected]'
To = $line.Email
Subject = "$($line.DBv) $($line.Server)"
Body = 'ÄäÖöÜü'
SmtpServer = 'smtp.mail.com'
Encoding = 'UTF8'
}

Send-MailMessage @sendMailMessageSplat

Variables work in a normal double-quoted body string, and they work in an HTML body as well. Just use BodyAsHtml = $true and construct the HTML as a string, for example: Body = "

Hello $($line.Name)

".

Answered By SilverKite63 On

A minimal test looks like this:

$params = @{
From = '[email protected]'
To = '[email protected]'
SmtpServer = 'smtp.example.com'
Subject = 'Test with ä'
Body = 'This is a test: ÄäÖöÜü'
Encoding = 'UTF8'
}
Send-MailMessage @params

Also, avoid rewriting the CSV just to add headers if possible. Import-Csv supports a Header parameter, which reduces the chance of changing the file’s encoding accidentally.

Answered By QuietMaple18 On

If the CSV is involved, use the same known encoding when reading and writing it. In Windows PowerShell 5.1, saving the script as UTF-8 with a BOM can also prevent hardcoded non-ASCII characters from being misread. For example:

$encoding = 'UTF8'
$P = Import-Csv -Path $CSVLocation -Delimiter ',' -Encoding $encoding

Then use Encoding = $encoding in the Send-MailMessage parameters. If the umlauts are hardcoded and the CSV does not contain them, Send-MailMessage’s encoding and the script file’s encoding are the important parts.

Answered By AmberTrolley5 On

Replacing ü with ue is a possible German-language fallback, but it isn’t necessary if the message is encoded correctly. UTF-8 should preserve the original characters. One longer-term consideration is that Send-MailMessage is deprecated, so a mail service API or another supported SMTP solution would be safer when you have the option to replace it.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.