r/PowerShell • u/PhiodorTiger • Jul 14 '26
Solved How to send 'ä' with Send-MailMessage
Hello Everybody, right now I am writing a Script to send out Informationmails fed with data from a CSV to Users inside our Company. (While I know this cmdlet is obsolete for now it has to do)
Everything works really well apart from the German 'Umlaute' (ÄäÖöÜü) those get "translated" to '??' in the message the recipients get and I don't know how to fix this.
I tried with BodyasHtml, but then my script just stops working entirely (User Error not completely unlikely) and also can I use Variables inside the Bodyashtml?
I also tried with giving it different encodings but none worked sadly
With normal sent mails our Outlook has no problem with those, just from the script.
Do you have any Ideas/Hints/Tips on what I can try?
Thank you very much in advance
Edit: Added Script (just removed any Private Information and implified the Body)
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.InitialDirectory = Get-Location
$dialog.Filter = "CSV-Dateien (_.csv)|_.csv"
$dialog.ShowDialog()
$CSVLocation = $dialog.FileName
(Get-Content $CSVLocation -Raw) -replace '^.*', 'DBv,BSv,RAM,Cpucount,Server,Name,Email,Service' | Set-Content $CSVLocation
$P = Import-Csv -Path $CSVLocation -Delimiter ','
foreach ($line in $P){
$sendMailMessageSplat = @{
From = 'johndoe@mail.com'
To = $line.Email
Subject = "$($line.DBv) $($line.Server)"
Body = "ÄäÖöÜü"
SmtpServer = 'smtp.mail.com'
}
Send-MailMessage
}
9
u/korvolga Jul 14 '26
save the script as utf8 BOM ?
1
u/Fischelsberger Jul 14 '26
Would be a good thing to include your actual command in here, so we could see what you currently doing.
Maybe you just need to include
-Encoding, probablyutf8could do the trick.https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.6#-encoding
1
u/8zaphod8 Jul 14 '26
If the data imported to Powershell shows the umlaut correctly, you could try replacing them by German umlaut HTML codes like ä for ä before sending out your mail. With HMTL body, of course.
If the problem is in the import from CSV to PowerShell, you should try UTF8 encoding.
1
u/lan-shark Jul 14 '26 edited Jul 14 '26
For your actual script, are you sending raw text in the body like that or are you creating the body based on the contents of the file? Because the answer may differ a bit depending on which you're doing. People are probably correct about the encoding but if your special characters are coming from the CSV then try specifying the encoding each time you read/write/send the text.
If you're hard coding your special characters in the string body, you may just need the
-Encodingparameter forSend-MailMessage. You may also need to save the script as that same encoding as mentioned by another commenter.EDIT: You potentially also have the option of attaching the CSV using
Send-MailMessage's-Attachmenhsparameter or creating the body as HTML and using-BodyAsHtml. It's hard to tell exactly what will work since you posted a truncated version of your script2
u/rumham_86 Jul 14 '26
Few things. Your splat isn’t working as I don’t see splat parameters being passed to send-mailmessage
Add utf8 encoding everywhere to prevent ansi overwrite.
Save your ps5.1 script as utf8 with bom.
Import-csv add encoding utf8 parameter
Send-mailmessage as well.
If you getting ?? Means ansi overwrite happening somewhere. Add encoding UTF8 to all 3 and should fix it
2
u/x0n Jul 14 '26
add: Encoding = "utf8" to your splat variable.
0
u/sup3rmark Jul 14 '26
First off, Send-MailMessage is, despite the lack of a real better function, deprecated as it's no longer considered secure. Depending on your mail system, there may be a REST-based way to send mail. Not in any way an answer to your question, but nobody else has mentioned it, so figured it was worth noting.
A workaround for this could be to replace your umlaut-laden vowels with the non-umlaut equivalent and tack an
eafter it. In German specifically, this is an even exchange: instead ofÄäÖöÜü, you'd haveAeaeOeoeUeue. That said, it's important to remember that this is not a universal solution, as not all languages that use umlauts (looking at you, Hungarian!) accept this. In Hungarian, this would change the spelling; the correct move in the case of Hungarian would be to just replace the umlauted character with the non-umlauted character and not add ane. But if you're dealing specifically with German and not worried about Hungarian, this is an acceptable solution.Ultimately, this is just a simple encoding issue as everyone else has mentioned, but I just wanted to highlight that there's often more than one way to dress a hot dog.
1
u/HeyDude378 Jul 15 '26
I opened up PowerShell and put:
This worked for me. I received A's, O's, and U's with umlauts.
1
u/snarkcheese Jul 16 '26
Not sure if it helps in this specific case but you can use character codes to reference the characters too.
0
u/mobani Jul 14 '26
It should work with -Encoding UTF8
-20
u/[deleted] Jul 14 '26
[removed] — view removed comment